formsvue.jsvuetify.jskeydown

Problems with with keyup.enter on vuetify v-text-field


I have a v-text-field from a vuetify form that calls a method but when I use the @keydown.enter event it does not work. With other keys, for example @keydown.esc, and with the button <v-btn @click="submit"> it works. The vuetify version is vuetify@2.3.21. The following example illustrates the problem:

<form>
  <v-text-field
    v-model="sepalLength"
    required
    @keydown.enter="submit" # HERE IS THE PROBLEM!!!
  ></v-text-field>  
  <v-btn @click="submit">Search</v-btn> # HERE IT WORKS
</form> 
<p style="white-space: pre-line;" v-if="predictedClass">{{ predictedClass }}</p>

<script>
data: () => ({
      predictedClass : ''
    }),
    methods: {
    submit () {
      axios.post('http://127.0.0.1:5000/predict', { 
        sepal_length: this.sepalLength
      })
      .then((response) => {
        this.predictedClass = response.data.class
      })
</script>


Solution

  • try either this

    <form>
      <v-text-field
        v-model="sepalLength"
        required
        @keydown.enter="submit" # HERE IS THE PROBLEM!!!
      ></v-text-field>  
      <v-btn @click="submit">Search</v-btn> # HERE IT WORKS
    </form> 
    <p style="white-space: pre-line;" v-if="predictedClass">{{ predictedClass }}</p>
    
    <script>
    data: () => ({
          predictedClass : ''
        }),
        methods: {
        submit (event) { //<---- add event param
          event.preventDefault() //<---------------HERE
          axios.post('http://127.0.0.1:5000/predict', { 
            sepal_length: this.sepalLength
          })
          .then((response) => {
            this.predictedClass = response.data.class
          })
    </script>
    

    or the short hand

    <form>
      <v-text-field
        v-model="sepalLength"
        required
        @keydown.enter.prevent="submit" <----------prevent here
      ></v-text-field>  
      <v-btn @click="submit">Search</v-btn> # HERE IT WORKS
    </form> 
    <p style="white-space: pre-line;" v-if="predictedClass">{{ predictedClass }}</p>
    
    <script>
    data: () => ({
          predictedClass : ''
        }),
        methods: {
        submit () {
          axios.post('http://127.0.0.1:5000/predict', { 
            sepal_length: this.sepalLength
          })
          .then((response) => {
            this.predictedClass = response.data.class
          })
    </script>