Prevent form submitting when pressing enter from a text input, using Vue.js

时间:2015-06-26 10:08:43

标签: javascript html vue.js

I am using Vue.js in my app and have a text input within a form

<div id="myVueForm">
<form>
   <input type="text"  v-on="keyup:addCategory | key 'enter'">

   <!-- more form fields -->
   <button type="submit">Submit Form</button>
</form>
</div>

In my Vue instance I have the following

new Vue({
    el: '#myVueForm',

    methods: {
        addCategory: function(e)
        {
           if(e) e.preventDefault();
           console.log("Added a new category, thanks!");
        }
    }
});

Despite the preventDefault(); call, when a user presses enter whilst on the text input the form still submits (although the addCategory() method does fire). This behaviour can be demonstrated in this fiddle.

I know I can use jQuery to catch the event and prevent the submit, but I'd like to see if it's possible in Vue to do this as it seems quite a common usage.

10 个答案:

答案 0 :(得分:34)

The submit is always fired on keydown. So use keydown instead of keyup.

<input type="text"  v-on="keydown:addCategory | key 'enter'">

答案 1 :(得分:31)

这是Vue.js 2.x的解决方案:

<input type='text' v-on:keydown.enter.prevent='addCategory' />

答案 2 :(得分:22)

为什么不禁用表单提交?

<form v-on:submit.prevent><input .../></form>

答案 3 :(得分:2)

您可以使用此:

HTML

<form name="..." @submit.prevent="onSubmitMethod">

JS

methods: {
  onSubmitMethod() {

  }
}

答案 4 :(得分:2)

对于那些只是希望阻止输入表单而又不想在输入上触发任何方法的人,只需执行以下操作:

<input type="text" @keypress.enter.prevent />

或者在自定义组件上,包含native修饰符:

<custom-input type="text" @keypress.enter.native.prevent />

精美可读,简洁明了。

答案 5 :(得分:1)

您可以用于禁用提交事件:

<form @submit.prevent="">

,然后当您需要提交表单时,请使用以下内容:

<button type="submit" @click="mySubmitMethod"> Send </button>

答案 6 :(得分:1)

我遇到了一个极端情况,其中输入在弹出窗口内,而弹出窗口仅用于表单的一部分。我想防止在打开弹出窗口时回车提交表单,但不要完全这样做。这可行:

<input type="text" @keydown.enter.prevent = "" /> 

答案 7 :(得分:1)

这有效

 <input type="text" class="form-control" v-model="get_user" @keydown.enter.prevent = "">

答案 8 :(得分:0)

您可以从HTML获取事件并将其发送到Vue,只是为了防止默认:

HTML

<input type="text" v-on:keydown.13="my_method($event)">

JS

methods: {
    my_method(event){
        // do something
        if (event) event.preventDefault();
        if (event) event.stopPropagation();
    },
},

答案 9 :(得分:0)

我为此写了一个助手。

export const preventFormSubmitOnEnter = {
  mounted() {
    let cb = event => {
      if (event) event.preventDefault();
    };
    if (this.$el.nodeName.toLowerCase() === "form") {
      this.$el.onsubmit = cb;
    } else {
      const forms = this.$el.getElementsByTagName("form");
      for (let i = 0; i < forms.length; i++) {
        const form = forms[i];
        if (form) form.onsubmit = cb;
      }
    }
  }
};

在您的vue组件中包含此mixin,它将自动运行。

这是一个示例(我使用ElementUI):

<template>
  <el-form :model="form" :rules="rules" ref="form">
    <el-form-item prop="reference">
      <el-input v-model="form.reference" placeholder="Your Reference"/>
    </el-form-item>
  </el-form>
</template>

<script>
import { preventFormSubmitOnEnter } from "./util";

export default {
  mixins: [preventFormSubmitOnEnter],
  data() {
    return {
      form: {
        reference: ""
      },
      rules: {
        reference: [{ required: true, message: "Reference is required!" }]
      }
    };
  },
  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.form.validate(valid => {
          this.$emit("on-validate", valid, this.form);
          resolve(valid);
        });
      });
    },
    validateField(prop) {
      this.$refs.form.validateField(prop);
    }
  }
};
</script>