我有10个复选框,如果我选择5个复选框,则应禁用其余复选框,如果我选择少于5个复选框,则应启用。如何使用JavaScript?
这是我的代码:
var count = 1;
function myfun(obj) {
debugger; /*var x = req.body.checkbox; if (x == undefined) { x = false;}*/
if ((obj.checked) == true) {
count++;
} else if ((obj.checked) == false) {
count--;
}
if (count > 5) {
c1.disabled = true;
c2.disabled = true;
c3.disabled = true;
c4.disabled = true;
c5.disabled = true;
c6.disabled = true;
c7.disabled = true;
}
}
答案 0 :(得分:1)
从描述的问题我得出了一个解决方案。看它适合你 https://jsfiddle.net/oL48u5ew/1/
HTML
<div class="parent">
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
<input type="checkbox" class="visit"/>
</div>
JS
var count = 0
$('.visit').change(function(){
if(this.checked)
count++
else
count --
if(count > 4)
{
$('.parent').find('.visit').each(function(){
if(!this.checked){
$(this).attr("disabled", true);
}
})
}
});