简体   繁体   English

@ keyup.enter意外结果

[英]@keyup.enter unexpected result

I have a single form with two vue 2.0 components. 我有一个包含两个vue 2.0组件的表单。 On both components I have a single input field, combined with a @keyup.enter: 在这两个组件上,我都有一个输入字段,并带有一个@ keyup.enter:

(Component#1) @keyup.enter="addItem" (组件1)@ keyup.enter =“ addItem”

(Component#2) @keyup.enter="addVariantItem" (组件2)@ keyup.enter =“ addVariantItem”

If the focus is on one of the inputs and I press enter, BOTH functions addItem and addVariantItem are called. 如果焦点在输入之一上,然后按Enter,则将调用函数addItem和addVariantItem。

 <template> <input type="text" class="form-control form-control-sm" id="variant-sku" placeholder="SKU" v-model="variantItemSku" @keyup.enter="addVariantItem()"> </template> <script> export default { data: function (){ return { variantItemSku: '' }; }, methods: { addVariantItem: function () { axios .post('/articles/' + this.article + '/variants', { sku: this.variantItemSku }) } } } </script> <style scoped> </style> 

 <template> <input type="text" class="form-control form-control-sm" id="cross-seller-sku" placeholder="SKU" v-model="crossSellerSku" @keyup.enter="addItem()"> </template> <script> export default { data: function (){ return { crossSellerSku: '' }; }, methods: { addItem: function () { axios .post('/articles/' + this.article + '/cross-sellers', { sku: this.crossSellerSku }) } } } </script> <style scoped> </style> 

ie I am in the "variant-sku" input and hit enter, addVariantItem() gets called, but also addItem in the other component ... Other way around, focus on "cross-seller-sku" causes the same result. 即我在“ variant-sku”输入中,然后按回车键,调用了addVariantItem(),但在其他组件中也调用了addItem。反之,专注于“ cross-seller-sku”会导致相同的结果。

Is there some way to limit the @keyup.enter to the current focussed input? 有什么方法可以将@ keyup.enter限制为当前的焦点输入? Right now it only works if one of the two inputs is focussed, if there is no focus nothing happens. 现在,只有两个输入中的一个被聚焦,它才有效;如果没有聚焦,则什么也没有发生。

Is there a bug, so that the keyup.enter is called on every input? 是否有错误,所以每次输入都会调用keyup.enter吗?

解决方案是将@ keyup.enter =“ function”更改为@ keydown.enter.prevent =“ function”,因为输入的默认行为在@keydown上起作用,而不是在@keyup上起作用,您必须覆盖它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM