【发布时间】:2020-02-04 13:39:27
【问题描述】:
所以我知道如何切换组中的所有单选按钮,但我这里有一个小众案例。我将尽我所能用代码和 cmets 来解释。
SomeComponent.vue
<template>
<div>
<!-- This checkbox will toggle the selected stated of all radios -->
<input class="toggleAll" type="checkbox" :checked="checked" @change="toggleAll">
<!-- These will either toggle their own checked state or
IF the above checkbox has been checked and all checkbox are selected, it would remove the
checked state from that one
as not all the radios ARE selected anymore -->
<input :checked="checked" class="radio" type="checkbox">
<input :checked="checked" class="radio" type="checkbox">
<input :checked="checked" class="radio" type="checkbox">
<input :checked="checked" class="radio" type="checkbox">
</div>
</template>
<script>
export default {
name: 'SomeComponent',
data() {
return {
checked: false,
}
},
methods: {
toggleAll() {
this.checked = !this.checked;
}
}
}
</script>
所以基本上,具有 toggleAll 类的输入将选中/取消选中所有复选框。但是,选中其他任何一个都应该取消选中您单击加toggleAll复选框的那个,因为不再选中所有复选框。
我似乎无法让它工作!我基本上需要说用 toggleAll 类查找输入并将数据“检查”更改为 false 但我不知道该怎么做。
TIA
【问题讨论】:
-
您不能依赖单个布尔值来驱动每个复选框的状态。每个复选框都必须有自己的数据元素,或者至少有自己的模型变量。然后它就像检查何时更改一样简单,如果未选中,则取消选中全选。
-
OK 那么如何检查子组件的数据是否发生了变化?
-
如果你在 Vue 中工作,为什么不使用
v-radio组件而不是标准的input?
标签: javascript vue.js radio-button