admin管理员组文章数量:1310067
I have group of checkboxes:
<div class="additions">
<input type="checkbox" value="10" v-model="additional">
<input type="checkbox" value="30" v-model="additional">
<div class="group">
<input type="checkbox" value="50" v-model="additional">
<input type="checkbox" value="70" v-model="additional">
</div>
</div>
I collecting checked values to data:
new Vue({
el: '#app',
data() {
return {
additional: [],
}
},
});
Can't figure out how to prevent checking more then 1 checkbox inside .group I tried using radio, but then stranger things e up, decided to stick with checkboxes. I could do it in jQuery or even vanilla JS I think, but I don't know where to put check (on change event method?). What is the best way to do it in VueJS?
Here is my pen:
I have group of checkboxes:
<div class="additions">
<input type="checkbox" value="10" v-model="additional">
<input type="checkbox" value="30" v-model="additional">
<div class="group">
<input type="checkbox" value="50" v-model="additional">
<input type="checkbox" value="70" v-model="additional">
</div>
</div>
I collecting checked values to data:
new Vue({
el: '#app',
data() {
return {
additional: [],
}
},
});
Can't figure out how to prevent checking more then 1 checkbox inside .group I tried using radio, but then stranger things e up, decided to stick with checkboxes. I could do it in jQuery or even vanilla JS I think, but I don't know where to put check (on change event method?). What is the best way to do it in VueJS?
Here is my pen: https://codepen.io/RomkaLTU/pen/LXOJgr?editors=1010
Share Improve this question edited Apr 5, 2022 at 14:54 Penny Liu 17.5k5 gold badges86 silver badges108 bronze badges asked Nov 20, 2018 at 9:40 RomkaLTURomkaLTU 4,1288 gold badges47 silver badges68 bronze badges 3-
according to your codepen, I saw the lables Radio #1 and Radio #2, are they meant to be
type="radio"
rather thantype="checkbox"
? – Tu Nguyen Commented Nov 20, 2018 at 9:46 - Sorry not radio, it was at the beginning but then I decided to stick with checkboxes. That was question about. – RomkaLTU Commented Nov 20, 2018 at 9:49
- check if this is what you are trying to achieve. codepen.io/Haeeb098/pen/eQebgz?editors=1011 – Haseeb Rehman Commented Nov 20, 2018 at 10:09
4 Answers
Reset to default 2You can use different ways:
1. :disabled
directive
<input type="checkbox" value="20" v-model="additional" :disabled="condition">
Using condition like additional.length > 0
you can disable checkbox if more then one already selected.
2. Watchers
data() {
...
},
watch: {
additional(newValue, oldValue) {
// new additional array value will be here every time any checkbox switched
}
}
Don’t think about the DOM, don’t think about classes. Hard habit to break, I know.
<input type="checkbox" value="50" v-model="additional" :disabled="hasAdditional">
puted: {
hasAdditional() {
return this.additional.length > 0
}
}
Use that as a starter for what you’re trying to do. Possibly you have to use a watcher to detect when it changes and uncheck ones that aren’t allowed. You could also change hasAdditional
to return the sum of the number of additions, then use that to work if if you can select the group.
Don’t rely on CSS classes. Use methods, watchers, and puted properties to work the logic out.
Thanks for pointing me out, but I choosed solution without disabling input as it get's very confusing for the end user. What I did:
<input type="checkbox" value="30" v-model="additional">
<input type="checkbox" value="40" v-model="additional">
<input type="checkbox" value="10" v-model="additional_grouped" @change="uniqueCheck">
<input type="checkbox" value="20" v-model="additional_grouped" @change="uniqueCheck">
new Vue({
el: '#app',
data() {
return {
additional: [],
additional_grouped: [],
}
},
puted: {
final: function(){
return this.additional.concat(this.additional_grouped);
}
},
methods: {
uniqueCheck(e){
this.additional_grouped = [];
if (e.target.checked) {
this.additional_grouped.push(e.target.value);
}
}
}
});
Just create a function to clear the list of check boxes each time you select an option:
<div class="additions">
<input type="checkbox" value="10" v-model="additional" v-on:click= "check_one()">
<input type="checkbox" value="30" v-model="additional" v-on:click= "check_one()">
</div>
<script>
data(){
return {
additional: [],
}
},
methods: {
check_one: function(){
this.additional = [];
}
}
}
</script>
本文标签: javascriptVueJS allow only 1 checkbox select based on class nameStack Overflow
版权声明:本文标题:javascript - VueJS allow only 1 checkbox select based on class name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741855241a2401305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论