【发布时间】:2018-08-06 11:47:54
【问题描述】:
我正在使用 VueJS,但在一系列复选框上实现两个功能时遇到问题。
- 首先,我的复选框是动态构建的。
- 我需要实现一个主“切换”复选框,单击该复选框时会选中或取消选中所有其他复选框。
- 我需要在模型中保留一组选中项。
此脚本为我提供了切换复选框,但不填充“checkedNames”数组。我尝试在名称复选框中添加一个 v-model,但它只是检查了所有内容,但仍然没有填充数组。
new Vue({
el: '#boxes',
data: {
checked: null,
checkedNames: [],
teams: [{
name: 'team a',
id: 'team-a',
checked: null,
members: [{
'id': 1,
'name': 'Dave'
},
{
'id': 2,
'name': 'Dee'
},
{
'id': 3,
'name': 'Dozy'
},
{
'id': 4,
'name': 'Beaky'
},
{
'id': 5,
'name': 'Mick'
},
{
'id': 6,
'name': 'Tich'
}
]
},
{
name: 'team b',
id: 'team-b',
checked: null,
members: [{
'id': 7,
'name': 'John'
},
{
'id': 8,
'name': 'Paul'
},
{
'id': 9,
'name': 'George'
},
{
'id': 10,
'name': 'Ringo'
}
]
}
]
}
});
<div id='boxes'>
<ul>
<li v-for="team in teams" style="width: 100px; float:left">
<div>
<input type="checkbox" class="form-check form-check-inline" v-bind:id="team.id" v-model="team.checked">
<label v-bind:for="team.id"><strong>{{ team.name }}</strong></label>
</div>
<ul class="countries_list">
<li v-for="member in team.members">
<input type="checkbox" class="form-check form-check-inline" v-bind:id="member.id" v-bind.checked="{checked : team.checked}">
<label v-bind:for="member.id">{{ member.name }}</label>
</li>
</ul>
</li>
</ul>
<br>
<span>Checked names: {{ checkedNames }}</span>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
https://jsfiddle.net/50wL7mdz/133578/
有人能指出我正确的方向吗?
【问题讨论】:
-
如果两个团队中都有
Ringo怎么办? -
这是可能的,但它会是一个不同的 Ringo。即,它将有一个唯一的 id。成员将始终只属于一个团队
标签: javascript html arrays vue.js