简体   繁体   English

多个复选框选定的元素传递到数组并从另一个数组显示在 vue js 中选中

[英]Multiple Checkbox selected elements pass to array and from another array show selected as checked in vue js

Am very new to vue js and need a way where I have multiple checkboxes and when I select them an array gets updated with the checked elements with Vue JS.我对 vue js 非常陌生,需要一种方法,让我有多个复选框,当我选择它们时,数组会使用 Vue JS 的选中元素进行更新。 Eg :例如:

allcolors = ['Red', 'Blue', 'Green']; and I select first two so my我选择前两个,所以我的

selectedColors = ['Red', 'Blue']; and then click saves, saves it to database.然后单击保存,将其保存到数据库。

When I open the form Red and Blue gets checked fetching record from database.当我打开表单时,红色和蓝色会检查从数据库中获取记录。

fetchedColors = ['Red', 'Blue'];

 new Vue({ el: "#app", data: { items: [ { label: 'Red', value: 'red' }, { label: 'Blue', value: 'blue' }, { label: 'Green', value: 'green' }, ], checkedValues: [], }, })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <label v-for="(item, index) in items" :key="index" > {{ item.label }} <input type="checkbox" :value="item.value" v-model="checkedValues" /> </label> {{ checkedValues }} </div>

Not really sure what you're asking.不太确定你在问什么。 Do you mean something like this?你的意思是这样的吗? Check the docs they may help - https://v2.vuejs.org/v2/guide/forms.html#Checkbox检查他们可能有帮助的文档 - https://v2.vuejs.org/v2/guide/forms.html#Checkbox

 new Vue({ el: "#app", data: { items: [ { label: 'Red', checked: true }, { label: 'Blue', checked: true }, { label: 'Green', checked: false }, ], }, })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <label v-for="(item, index) in items" :key="index" > {{ item.label }} <input type="checkbox" :checked="item.checked" @change="({ target: { checked }}) => items[index].checked = checked" /> </label> </div>

If you want to show checkboxes that are checked when the form displays you could do something like this.如果您想显示表单显示时选中的复选框,您可以执行类似的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM