Vuejs:多输入的v模型数组

Jan*_*ndt 40 arrays vue.js

我有一个带有v模型的输入文本字段,每当有人点击"添加"按钮时,另一个输入文本会添加到DOM,并附加相同的v模型.我想我会得到一个v模型值的数组,但它只获得第一个v模型输入的值:

<div id="app">
  <div id="references">
    <input v-model="references" type="text">
  </div>
  <button @click="addReference">Add</button>
</div>
Run Code Online (Sandbox Code Playgroud)

我附加到dom的html是由addReference方法触发的:

addReference: function(e) {
  e.preventDefault();
  console.log(this.references);
  var inputEl = '<input v-model="references" type="text">';
  $('#references').append(inputEl);
}
Run Code Online (Sandbox Code Playgroud)

这是Vuejs不能做的事情吗?是否有不同的方法从动态dom元素中收集Vuejs的值?

new Vue({
  el: "#app",
  data: {
    references: "text"
  },
  methods: {
    addReference: function(e) {
      e.preventDefault();
      console.log(this.references);
      var inputEl = '<input v-model="references" type="text">';
      $('#references').append(inputEl);
    }
  }
})
Run Code Online (Sandbox Code Playgroud)
input {
  display: block;
  margin: 1px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="app">
  <div id="references">
    <input v-model="references" type="text">
  </div>
  <button @click="addReference">Add</button>
</div>
Run Code Online (Sandbox Code Playgroud)

Bil*_*ell 62

你在想DOM,这是一个很难打破的习惯.Vue建议您先处理数据.

在你的确切情况下很难分辨,但我可能会使用a v-for并制作一个数组finds来推动,因为我需要更多.

这是我如何设置我的实例:

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

以下是我设置模板的方法:

<div id="app">
  <h1>Finds</h1>
  <div v-for="(find, index) in finds">
    <input v-model="find.value" :key="index">
  </div>
  <button @click="addFind">
    New Find
  </button>
</div>
Run Code Online (Sandbox Code Playgroud)

以下是上述演示:https://jsfiddle.net/a5t24gm3/1/

  • 啊哈!因此,通过创建一个数组,它允许您创建一个 v-for,您可以告诉 vue 它将是一个数组,而不是期望 v-model 单值到 vuejs 的数组值魔术!谢谢一堆!绝对喜欢 Vuejs。 (2认同)

Yev*_*yev 18

如果您在vue2中询问如何执行此操作并选择插入和删除它,请查看js小提琴

new Vue({
  el: '#app',
  data: {
    finds: [] 
  },
  methods: {
    addFind: function () {
      this.finds.push({ value: 'def' });
    },
    deleteFind: function (index) {
      console.log(index);
      console.log(this.finds);
      this.finds.splice(index, 1);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue@2.5.3/dist/vue.js"></script>
<div id="app">
  <h1>Finds</h1>
  <div v-for="(find, index) in finds">
    <input v-model="find.value">
    <button @click="deleteFind(index)">
      delete
    </button>
  </div>
  
  <button @click="addFind">
    New Find
  </button>
  
  <pre>{{ $data }}</pre>
</div>
Run Code Online (Sandbox Code Playgroud)


saj*_*jad 5

这是上面的演示:https : //jsfiddle.net/sajadweb/mjnyLm0q/11

new Vue({
  el: '#app',
  data: {
    users: [{ name: 'sajadweb',email:'sajadweb@outlook.com' }] 
  },
  methods: {
    addUser: function () {
      this.users.push({ name: '',email:'' });
    },
    deleteUser: function (index) {
      console.log(index);
      console.log(this.finds);
      this.users.splice(index, 1);
      if(index===0)
      this.addUser()
    }
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
  <h1>Add user</h1>
  <div v-for="(user, index) in users">
    <input v-model="user.name">
    <input v-model="user.email">
    <button @click="deleteUser(index)">
      delete
    </button>
  </div>
  
  <button @click="addUser">
    New User
  </button>
  
  <pre>{{ $data }}</pre>
</div>
Run Code Online (Sandbox Code Playgroud)