返回

Vue.js中使用input元素监听回车键

日志

<template>
  <input type="text" @keyup.enter="handleEnter">
</template>

<script>
export default {
  methods: {
    handleEnter() {
      // Do something when the user presses Enter
    }
  }
}
</script>

你也可以使用修饰符 @keyup.enter.prevent 来防止回车键提交表单:

<template>
  <form @keyup.enter.prevent="handleEnter">
    <input type="text">
    <button type="submit">Submit</button>
  </form>
</template>

<script>
export default {
  methods: {
    handleEnter() {
      // Do something when the user presses Enter
    }
  }
}
</script>