我总共有6个复选框(将来可能会添加更多)并且我想只允许选择一个,所以当用户选中其中任何一个时,其他应该取消选中.
我尝试使用这段代码并且如果我定义了ID,但是现在只是想知道如何以有效的方式使它反之亦然,以便将来如果我添加更多而不是一个问题
$('#type1').click(function() {
$('#type2').not('#type1').removeAttr('checked');
});
Run Code Online (Sandbox Code Playgroud)
仅供参考,复选框不是兄弟姐妹,而且不同 <td>
bil*_*can 154
绑定一个change处理程序,然后取消选中所有复选框,除了选中的复选框:
$('input.example').on('change', function() {
$('input.example').not(this).prop('checked', false);
});
Run Code Online (Sandbox Code Playgroud)
Sud*_*oti 15
您可以在所有复选框中使用类,并执行以下操作:
$(".check_class").click(function() {
$(".check_class").attr("checked", false); //uncheck all checkboxes
$(this).attr("checked", true); //check the clicked one
});
Run Code Online (Sandbox Code Playgroud)
尝试这个
$(function() {
$('input[type="checkbox"]').bind('click',function() {
$('input[type="checkbox"]').not(this).prop("checked", false);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
111691 次 |
| 最近记录: |