如何检查和取消选中div中的所有复选框

jon*_*nes 2 html javascript checkbox jquery

我想取消选中'test'div中的所有checkboxex,而不选择那些属性被禁用的复选框.

<div id="test">
   <input type="checkbox" id="check1" name="check1">
   <input type="checkbox" id="check01" name="check01" disabled="disabled">
   <input type="checkbox" id="check2" name="check2" disabled="disabled">
   <input type="checkbox" id="check3" name="check3">
   <input type="checkbox" id="check4" name="check4">
   <input type="checkbox" id="check50" name="check50">
   <input type="checkbox" id="check6" name="check6">   
</div>
<input type="button" id="uncheckAll" onclick="uncheckAll('test')">
<script language="javascript">
 function uncheckAll() {
     $('#' + divid + ' :checkbox').attr('checked', false);
     /*
   This function uncheck all of the checkboxes, which i don't want, i want to
   uncheck only checkboxes whose attributes are not disabled.
  /*
}*/
</script>
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 15

您需要接受名为divId的参数,然后使用:enabled filter来过滤掉禁用的复选框

function uncheckAll(divid) {
    $('#' + divid + ' :checkbox:enabled').prop('checked', false);
}
Run Code Online (Sandbox Code Playgroud)

演示:小提琴


如果没有jQuery

function uncheckAll(divid) {
    var checks = document.querySelectorAll('#' + divid + ' input[type="checkbox"]');
    for(var i =0; i< checks.length;i++){
        var check = checks[i];
        if(!check.disabled){
            check.checked = false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

演示:小提琴