2015-07-13 69 views
0

我想限制取消所有3个复选框的能力。jquery禁用剩余的复选框时,2 3未经检查

<input type="checkbox" value="one" checked> one 
<input type="checkbox" value="two" checked> two 
<input type="checkbox" value="three" checked> three 


$(":checkbox").click(function() {        
    if ($("input:checked").length == 2){               
    $(':checkbox(:checked)').prop('disabled', false); 
    }else{               
    $(':checkbox(:checked)').prop('disabled', true); 
    }               
});  

我不能得到它来禁用最后一个复选框,因为没有被选中。 请帮忙!

http://jsfiddle.net/Vg4ty/81/

+0

为什么downvote? – BragDeal

+0

我没有downvote,但你的小提琴工作 – AmmarCSE

+0

不,它禁用其他两个再次检查!我应该只允许我取消选中3中的2个,但我应该始终能够检查未选中的项目 – BragDeal

回答

1

$(":checkbox").click(function() { 
 
    if ($("input:checked").length == 1) { 
 
    $(':checked').prop('disabled', true); 
 
    } else { 
 
    $(':checked').prop('disabled', false); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
 
<input type="checkbox" value="one" checked>one 
 
<input type="checkbox" value="two" checked>two 
 
<input type="checkbox" value="three" checked>three

+0

谢谢!就是这样 – BragDeal

0

捕获点击事件,然后阻止默认的行动,如果它会导致点击元素的数量是零。

$("input[type=checkbox]").on("click",function(evt) {    
    if ($("input[type=checkbox]:checked").length == 0) { 
     evt.preventDefault(); 
    }               
}); 
相关问题