Vuejs: v-model array in multiple input

前端 未结 3 1021
一个人的身影
一个人的身影 2021-01-30 20:16

I have an input text field with a v-model attached, and every time someone hits the \"Add\" button, another input text get added to the DOM with the same v-model attached. I tho

3条回答
  •  长发绾君心
    2021-01-30 20:43

    You're thinking too DOM, it's a hard as hell habit to break. Vue recommends you approach it data first.

    It's kind of hard to tell in your exact situation but I'd probably use a v-for and make an array of finds to push to as I need more.

    Here's how I'd set up my instance:

    new Vue({
      el: '#app',
      data: {
        finds: []
      },
      methods: {
        addFind: function () {
          this.finds.push({ value: '' });
        }
      }
    });
    

    And here's how I'd set up my template:

    Finds

    Although, I'd try to use something besides an index for the key.

    Here's a demo of the above: https://jsfiddle.net/crswll/24txy506/9/

提交回复
热议问题