vue.js 按回车键时如何防止此功能运行?

anauzrmj  于 2023-03-13  发布在  Vue.js
关注(0)|答案(3)|浏览(198)

我想在用户按回车键时停止checkingNric的运行。请帮助。

onPress(e){
  if(e.keyCode == 13){
    this.checkingNric(e.preventDefault());
  }
},
lskq00tm

lskq00tm1#

不知道我是否理解得很好。
这将在第一次按下enter后停止调用this.checkingNric()。
当您使用Vue时,添加enterWasPressed:将false变量添加到组件的data()。

onPress(e){
    if(e.keyCode == 13){
        this.enterWasPressed = true;
    }

    if (! this.enterWasPressed) {
        this.checkingNric(e.preventDefault());
    }
},
lymnna71

lymnna712#

可以使用Vue的关键点修改器,并像keydown.enter.prevent一样将它们链接起来。

new Vue({
  el: "#app",
  methods: {
    myMethod(e) {
      console.log(123);
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="myMethod" @keydown.enter.prevent="">Only click, no enter</button>
</div>
r9f1avp5

r9f1avp53#

在将事件作为参数传递之前运行preventDefault

onPress(e){
          if(e.keyCode == 13){
            e.preventDefault();
            this.checkingNric(e);
         }
    },

相关问题