简体   繁体   English

如何在输入集中时阻止表单提交,并在不集中时使用vuejs2启用表单提交?

[英]How to prevent form submit when input is focused and enable it when out of focused with vuejs2?

I am working on a form with a vue component that has an autocomplete functionality, I would like to let user select from suggestions. 我正在使用具有自动完成功能的vue组件处理表单,我想让用户从建议中进行选择。 I am handling keyup events for up, down and enter. 我正在处理向上,向下和进入的键盘输入事件。 Unfortunately, when enter is hit, the form is submitted. 不幸的是,当按下回车键时,将提交表单。 I would like to prevent form submit if the input field is focused. 如果输入字段集中,我想防止表单提交。 I have tried using v-on:submit.prevent in the form tag but that disables form submit completely and I need to create an separate form submit function. 我已经尝试在form标签中使用v-on:submit.prevent,但是它完全禁用了表单提交功能,我需要创建一个单独的表单提交功能。

You could do it like this. 您可以这样做。 But I don't really know vue so I might be wrong. 但是我不太了解vue,所以我可能是错的。

 window.onload = function() { new Vue({ data() { return { focused: false } }, methods: { onSubmit: function() { return this.focused; } } }).$mount('#app') } 
 <html> <head> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script> </head> <body> <div id="app"> <form v-on:submit="onSubmit"> <input type="text" @focus="focused = true" @blur="focused = false" /> <button :disabled="focused">Submit</button> </form> </div> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当输入框聚焦时禁用复选框启用 - Disabled checkbox enable when an input box focused Javascript:如何防止在窗口聚焦时触发对输入的聚焦? - Javascript: How to prevent triggering focus on input when window is focused? 输入焦点时如何删除标签 - How to remove tags when input is focused 关注时如何更改输入字段的位置? - How to change position of input field when focused on? 聚焦时将 class 添加到输入,未聚焦时将其移除 - Add class to an input when its focused, and remove it when isnt focused 在关注表单中的最后一个输入时自动添加行 - Add row automatically when focused on last input in the form 聚焦第一个输入时如何在表单上重置验证方法? (jQuery Ajax 表单插件 + Magnific Popup) - How to reset validation method on form when first input is focused? (jQuery Ajax Form Plugin + Magnific Popup) Material UI Input 的焦点和不焦点时无法更改边框颜色 - Unable to change border color when focused and not focused of Material UI Input 集中iframe时,不会调用父窗口事件。 怎么预防呢? - When the iframe is focused, the parent window events are not called. How to prevent it? JavaScript表单验证。 使用onfocus专注于输入字段时如何消除错误 - Javascript Form Validation. how to remove error when focused on a input field using onfocus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM