checkbox-group多项选择器,内部由多个checkbox组成;
属性bindchange 说明checkbox-group中选中项发生改变时触发 change 事件,detail = {value:[选中的checkbox的value的数组]};

1
2
3
4
5
6
<checkbox-group  class="checkbox_group" bindchange="checkboxChange">
<label class="checkbox" wx:for="{{items}}" wx:key="item">
<checkbox value="{{item.value}}" checked="{{item.checked}}" />
{{item.value}}
</label>
</checkbox-group>

每一次修改选中项,都需要一个空数组去存放选中项。

点我展示代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
checkboxChange(e) {
const items = this.data.items;
const values = e.detail.value;
// 设置选中的数组为空 选中的id push到数组里面
this.data.vehicle_arr = [];
for (let i = 0, lenI = items.length; i < lenI; ++i) {
items[i].checked = false // 遍历items,让所有的选中状态都为false
for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
if (items[i].value === values[j]) {
items[i].checked = true;
const List = items[i].name.toString().split();
for (let k in List) {
this.data.vehicle_arr.push(List[k]);
}
}
}
}
// 选中的id
this.data.unique = [...new Set(this.data.vehicle_arr)];
this.setData({
driverarr: e.detail.value.join(","), // 多选的数组
items,
});
},