admin管理员组文章数量:1192915
I need to limit number of selected checkboxes to 5. I tried using :disabled like here:
<ul>
<li v-for="item in providers">
<input type="checkbox"
value="{{item.id}}"
id="{{item.id}}"
v-model="selected_providers"
:disabled="limit_reached"
>
</li>
</ul>
And then:
new Vue({
el: '#app',
computed:{
limit_reached: function(){
if(this.selected_providers.length > 5){
return true;
}
return false;
}
}
})
This kind of works, but when it reaches 5, all of the check boxes are disabled and you can't uncheck the ones you don't want.
I also tried to splice the array with timeout of 1 ms, which works but feels hacky. Can anyone recommend anything please?
I need to limit number of selected checkboxes to 5. I tried using :disabled like here:
<ul>
<li v-for="item in providers">
<input type="checkbox"
value="{{item.id}}"
id="{{item.id}}"
v-model="selected_providers"
:disabled="limit_reached"
>
</li>
</ul>
And then:
new Vue({
el: '#app',
computed:{
limit_reached: function(){
if(this.selected_providers.length > 5){
return true;
}
return false;
}
}
})
This kind of works, but when it reaches 5, all of the check boxes are disabled and you can't uncheck the ones you don't want.
I also tried to splice the array with timeout of 1 ms, which works but feels hacky. Can anyone recommend anything please?
Share Improve this question asked Feb 2, 2016 at 0:01 AdamAdam 3,5184 gold badges34 silver badges53 bronze badges1 Answer
Reset to default 29Basically, you'd only disable the input if it's not already selected and there's more than 4 of them selected.
The cleanest way I could come up with was:
new Vue({
el: '#app',
data: {
inputs: []
}
})
With:
<ul>
<li v-for="n in 10">
<label>
<input
type="checkbox"
v-model="inputs"
:value="n"
:disabled="inputs.length > 4 && inputs.indexOf(n) === -1"
number> Item {{ n }} {{ inputs.indexOf(n) }}
</label>
</li>
</ul>
Here's a quick demo: https://jsfiddle.net/crswll/axemcp8d/1/
本文标签: javascriptVuejs limit selected checkboxes to 5Stack Overflow
版权声明:本文标题:javascript - Vue.js limit selected checkboxes to 5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738473997a2088755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论