简体   繁体   中英

Checking parent checkbox doesn't check child

I am trying to get some jquery to check the child checkbox when the parent is selected, however it is not happening and I can't for the life of me figure out what the deal is.

Code:

$('#check-all-parent').click(function(event) {
if(this.checked) { // check select status
    $('#check-all-child').each(function() { //loop through each checkbox
        this.checked = true;  //select all checkboxes with class "checkbox1"               
    });
}else{
    $('#check-all-child').each(function() { //loop through each checkbox
        this.checked = false; //deselect all checkboxes with class "checkbox1"                       
    });         
}  
});

HTML:

<input type="checkbox" id="check-all-parent">hello
<br />
<input type="checkbox" id="check-all-child">hello2

Here is the jsfidle

Use class instead of id.

HTML

<input type="checkbox" id="check-all-parent">hello
<br />
<input type="checkbox" class="check-all-child">hello2

jQuery

$('#check-all-parent').click(function(event) {
if(this.checked) { // check select status
    $('.check-all-child').each(function() { //loop through each checkbox
        this.checked = true;  //select all checkboxes with class "checkbox1"               
    });
}else{
    $('.check-all-child').each(function() { //loop through each checkbox
        this.checked = false; //deselect all checkboxes with class "checkbox1"                       
    });         
} 

Your code works, you just need to add jQuery in the jsfiddle. Click the first drop-down under Frameworks & Extensions, and choose a jQuery library (like this: jsfiddle.net/evx9y8g9/3 ).

Your JS could be a bit cleaner though:

$('#check-all-parent').click(function() {
    $("#check-all-child").prop("checked", $(this).prop("checked"));
});

No need to use loop . Use class instead of id and in js:

 $(".check-all-parent").on("click", function() { $(".check-all-child").prop("checked", $(this).prop("checked")); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" class="check-all-parent">hello <br /> <input type="checkbox" class="check-all-child">hello2 <br /> <input type="checkbox" class="check-all-child">hello2 <br /> <input type="checkbox" class="check-all-child">hello2 <br /> <input type="checkbox" class="check-all-child">hello2 

If you have parent an child checkboxes, consider using a nested list:

<ul id="checkboxes">
  <li>
    <input type="checkbox"> hello
    <ul>
      <li> <input type="checkbox"> hello1 </li>
      <li> <input type="checkbox"> hello2 </li>
    </ul>
  </li>
</ul>

And then use

$('#checkboxes input[type="checkbox"]').on('change', function() {
  $(this)
  .nextAll('ul')
  .find('input[type="checkbox"]')
  .prop('checked', this.checked);
});

 $('#checkboxes input[type="checkbox"]').on('change', function() { $(this) .nextAll('ul') .find('input[type="checkbox"]') .prop('checked', this.checked); }); 
 ul { list-style: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul id="checkboxes"> <li> <input type="checkbox"> hello <ul> <li> <input type="checkbox"> hello1 <ul> <li> <input type="checkbox"> hello11 </li> </ul> </li> <li> <input type="checkbox"> hello2 </li> <li> <input type="checkbox"> hello3 </li> </ul> </li> </ul> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM