【发布时间】:2018-03-22 03:43:25
【问题描述】:
我希望始终至少选中一个复选框,但我混合了 v-model 和 :checked 的概念。
doc 说:
v-model将忽略初始的value、checked或selected属性 在任何表单元素上都可以找到。它将始终处理 Vue 实例数据 作为真理的源泉。
我可以阻止我的模型被修改,但我不能阻止复选框被选中...
一些代码:
模板
<div class="wrapper" v-for="(s, id) in userOutputSeries" :key="id">
<input type="checkbox" :id="id" :value="id" @change="preventIfLessThanOneChecked" :checked="s.active">
<label :for="id">{{ s.display }}</label>
</div>
模特userOutputSeries
data () {
return {
userOutputSeries: {
foo: {
display: 'Awesome Foo',
active: true
},
bar: {
display: 'My Bar',
active: false
}
}
}
}
preventIfLessThanOneChecked 处理程序
preventIfLessThanOneChecked (event) {
// I don't update the model so it stay at the same state
// But only need to count the active series and do what we want.
console.log(event.target.value, event.target.checked)
}
有什么想法可以阻止本机复选框的传播吗?
【问题讨论】:
标签: checkbox vue.js vuejs2 v-model