javascript - 如何使用回车键将项目添加到输入中,并且仅在单击按钮时提交表单?

标签 javascript vue.js vuejs2 vue-component v-select

我正在使用 v-select,但这个插件有问题...当您在输入中按 enter 时,它会提交到表单。如何使用 Enter 将项目添加到输入中,并且仅在单击按钮时提交表单?

Example Here CODE PEN

HTML

<div id="app">
  <h1>Vue Select</h1>
  <p>Try to add items in input using "ENTER"</p>
  <form v-on:submit.prevent="submited()">
  <v-select multiselect :options="options"></v-select>
    <button type="submit">Submit</button>
  </form>
</div>

JS

Vue.component('v-select', VueSelect.VueSelect)

new Vue({
  el: '#app',
  data: {
    options: ["some", "thing", "another", "things"]
  },
  methods: {
    submited(){
      alert('submited!')
    }
  }
})

谢谢!!

最佳答案

我会阻止表单上的默认设置,然后将提交的逻辑移至按钮。

Vue.component('v-select', VueSelect.VueSelect)

new Vue({
  el: '#app',
  data: {
    options: ["some", "thing", "another", "things"]
  },
  methods: {
    submitted() {
      console.log('submited!')
    }
  }
})
body {
  font-family: 'Open Sans', sans-serif;
}

#app {
  max-width: 35em;
  margin: 1em auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<script src="https://unpkg.com/vue-select@2.2.0/dist/vue-select.js"></script>
<div id="app">
  <h1>Vue Select</h1>
  <p>Try to add items in input using "ENTER"</p>
  <form v-on:submit.prevent="">
    <v-select multiple :options="options"></v-select>
    <button type="button" v-on:click="submitted()">Submit</button>
  </form>
</div>

看这里: https://codepen.io/uidan/pen/PJjOyb

关于javascript - 如何使用回车键将项目添加到输入中,并且仅在单击按钮时提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492695/

相关文章:

javascript - 工作时间选择?

typescript - TypeScript Vue 组件中的泛型

javascript - 文件阅读器不会第一次加载 - VUE.JS

javascript - 如何在 Vue 中的渲染列表中打开模式?

javascript - 如何使用php和javascript在主键前添加八个0

vue.js - 在 HTML 页面中插入 VueJS 组件模板不起作用

javascript - D3 放大笔刷范围

vue.js - 在初始路由之前执行 Vuex 操作

javascript - 实现无限滚动的后退按钮

javascript - 通过在 Drupal 7 中使用 JQuery 更改背景图像 CSS 属性来进行幻灯片放映