如何为具有多个字段的对象数组做v模型



所以数组中的每个元素都有2个复选框输入。第一个表示表单中的字段第二个表示是否需要该字段,我需要构造一个对象数组,如

[
{
name: 'fieldName',
required: true
},
{
name: 'anotherFieldName'
}
]

如果第一个复选框被选中,我需要添加一个带有"名称"的对象。属性,如果第二个也被选中了我还需要添加& required"场。

try this

<li v-for="(item, index) of items"> 
{{ item.prop2 }} 
<input type="text" v-model="items[index].prop2">
</li>

<li v-for="(item, index) of items"> 
{{ item.prop2 }} 
<input type="text" @input="updateMyProp(index)">
</li>
...
methods: {
updateMyProp ($event, index) {
// your update logic here
// you can use 'this.items', Object.assign, Vue.set, etc... to update your value
}
...

最新更新