如果在vue中使用数组作为模型,如何获取复选框的选中值

Kua*_*uan 5 vue.js vuejs2

我想知道我是否使用数组作为多复选框列表的模型,如何有效地检查哪些项目已选中,哪些未选中,而不是与该数组一一比较?

<ul>
<li v-for="task in tasks">
<input type="checkbox" :id="task.title" :value="task" v-model="selectedTasks" @change="handleTasks(task)">
<label :for="task.title">{{task.title}}</label>
</li>
</ul>


new Vue({
  data: {
      tasks: [
        { title: 'Go to the store' },
        { title: 'Clean the house' },
        { title: 'Learn Vue.js' }
      ],
      selectedTasks: []
  },
})
Run Code Online (Sandbox Code Playgroud)

Dig*_*ter 3

使用循环索引:

<li v-for="(task, index) in tasks">
  <input type="checkbox" :id="task.title" :value="task" v-model="selectedTasks[index]" @change="handleTasks(task)">
  <label :for="task.title">{{task.title}}</label>
</li>
Run Code Online (Sandbox Code Playgroud)