代码之家  ›  专栏  ›  技术社区  ›  Johnson

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

  •  0
  • Johnson  · 技术社区  · 6 年前

    进来 在输入内部,它提交到表单。如何使用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!')
        }
      }
    })
    

    谢谢

    3 回复  |  直到 6 年前
        1
  •  1
  •   gautsch    6 年前

    我会防止表单上出现默认值,然后将提交的逻辑移动到按钮。

    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

        2
  •  1
  •   Roy J    6 年前

    <v-select multiple :options="options" @keypress.native.prevent=""></v-select>
    
        3
  •  1
  •   Oyewole A. Samuel    6 年前

       <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 :value.sync="selected" :options="options"></v-select>
            <button type="submit" @click="submitedd()">Submit</button>
          </form>
        </div>
    
    
    Vue.component('v-select', VueSelect.VueSelect);
    
    new Vue({
      el: '#app',
      data() {
        return {
          selected: null,
          options: ["some", "thing", "another", "things"]
        }
      },
      methods: {
        submitedd(){
          console.log("i am here");
        }
      }
    })