最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check and uncheck all checkboxes inside a div - Stack Overflow

programmeradmin4浏览0评论

I want to uncheck all checkboxex inside the 'test' div without those checkboxes whose attributes are disabled.

<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>

I want to uncheck all checkboxex inside the 'test' div without those checkboxes whose attributes are disabled.

<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>
Share Improve this question edited Mar 18, 2015 at 5:20 Anik Islam Abhi 25.4k8 gold badges59 silver badges81 bronze badges asked Mar 18, 2015 at 5:00 jonesjones 1,4534 gold badges37 silver badges76 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 19

You need to accept the param named divId then use :enabled filter to filter out disabled checkboxes

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

Demo: Fiddle


If no 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;
        }
    }
}

Demo: Fiddle

You can take a common class to each input. Try this

<div id="test">
            <input type="checkbox" id="check1" class="check" name="check1">
            <input type="checkbox" id="check01" class="check" name="check01" disabled="disabled">
            <input type="checkbox" id="check2" class="check" name="check2" disabled="disabled">
            <input type="checkbox" id="check3" class="check" name="check3">
            <input type="checkbox" id="check4" class="check" name="check4">
            <input type="checkbox" id="check50" class="check" name="check50">
            <input type="checkbox" id="check6" class="check" name="check6">   
        </div>
    <input type="button" id="uncheckAll" onclick="uncheckAll()">
<script language="javascript">
  function uncheckAll()
{
  $("input.check").each(function(){
    if($(this).prop('disabled')!=true)
    {
      $(this).attr('checked',false);
    }
  });

}
</script>

Following Single line jQuery code will do your work.

    function uncheckAll(divid) {
        $('#' + divid + ' :checkbox:not([disabled])').attr('checked', false);
    }

Simplest solution ever:

// index.html create this in <body>:

<button type="button" id="desativar-tudo">Uncheck All</button>
<button type="button" id="ativar-tudo">Check All</button>

// after that create this:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">         

function uncheckAll() {$('input').prop('checked', false);}
function CheckAll(){$('input').prop('checked', true);}

$('#desativar-tudo').click(uncheckAll);
$('#ativar-tudo').click(CheckAll);

</script>

Tested/Worked in 2024

$('.div_parent input[type="checkbox"]').prop('checked', false).trigger('change');

Details: give a class to the parent div of checkboxes "div_parent" and catch all checkboxes. trigger('change') is optional to trigger other logics related to checkboxes.

发布评论

评论列表(0)

  1. 暂无评论