简体   繁体   English

jQuery检查父复选框是否选中子复选框

[英]jQuery check the parent checkbox if the child checkbox is checked

I want to make a logic that if a child checkbox is checked, it checks the parent checkbox too. 我想提出一个逻辑,如果选中了子复选框,它也会同时选中父复选框。 The input can parent, child and both: 输入可以是父项,子项以及两者:

<input type="checkbox" name="checkbox[$counter]" class="parent" />
<input type="checkbox" name="checkbox[$counter]" class="child" />
<input type="checkbox" name="checkbox[$counter]" class="parent child" />

This structure: 这个结构:

  • 1 1个
    • 2 2
    • 3 3
  • 4 4
    • 5 5
      • 6 6
      • 7 7

will be look like this: 将如下所示:

<table class="table">
  <tr>
    <td>
      <input type="checkbox" name="checkbox[1]" class="parent" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[2]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[3]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[4]" class="parent" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[5]" class="child parent" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[6]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[7]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
</table>

I want if I check 3, it checks 1 and if I check 6 it checks 5 and 4, too. 我要检查3,检查1,如果检查6,检查5和4。

Also the checkboxes are in separate table rows, becouse I want to show some information structured in table next to the checkbox. 复选框也位于单独的表行中,因为我想在复选框旁边显示一些结构化的信息。

Can you show me a script what does it? 你能告诉我一个剧本是什么吗? I tried this, but it's not working: 我试过了,但是没有用:

$(document).ready(function() {
    $('input.child').change(function() {
        if ($(this).is(':checked')) {
            $(this).closest('input.parent:checkbox').attr('checked', true);
        }
    });
});

The checkboxes are not parent-child relationship, thus the method .closest() will not work as it traverse's up starting from itself. 复选框不是父子关系,因此.closest()方法将无法运行,因为它是从自身开始遍历的。

I would recommend you to assign classes with TR element and also attach event handler with them, traversing DOM will be easy. 我建议您为类分配TR元素,并为其附加事件处理程序,遍历DOM会很容易。

<tr class="parent">...</tr>
<tr class="child">...</tr>

You could use combination of .prevAll() and .first() to get the targeted TR , then use .find() to get the checkbox. 你可以使用组合.prevAll().first()来获得有针对性的TR ,然后使用.find()来获得的复选框。

Additionally, If you want to fire the change event trigger() method can be used. 此外,如果要trigger()更改事件,则可以使用trigger()方法。

 $(document).ready(function() { $('.child').on('change', ':checkbox', function() { if ($(this).is(':checked')) { var currentRow = $(this).closest('tr'); var targetedRow = currentRow.prevAll('.parent').first(); var targetedCheckbox = targetedRow.find(':checkbox'); targetedCheckbox.prop('checked', true).trigger('change'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table"> <tr class="parent"> <td> <input type="checkbox" name="checkbox[1]" /> </td> </tr> <tr class="child"> <td> <input type="checkbox" name="checkbox[2]" /> </td> </tr> <tr class="child"> <td> <input type="checkbox" name="checkbox[3]" /> </td> </tr> <tr class="parent"> <td> <input type="checkbox" name="checkbox[4]" /> </td> </tr> <tr class="child parent"> <td> <input type="checkbox" name="checkbox[5]" /> </td> </tr> <tr class="child"> <td> <input type="checkbox" name="checkbox[6]" /> </td> </tr> <tr class="child"> <td> <input type="checkbox" name="checkbox[7]" /> </td> </tr> </table> 

Try below script, 试试下面的脚本,

$(document).on('change', 'input[type=checkbox]', function(e) {
  $(this).siblings('ul').find("input[type='checkbox']").prop('checked', this.checked);
  $(this).parentsUntil('input[type=checkbox]').children("input[type='checkbox']").prop('checked', this.checked);
  e.stopPropagation();
});

With your current html structure you need make a link between childs and parents , for example with data attribute, now it works with any html structure, table , ul li or without these. 根据您目前的html结构,你需要做之间的联系childsparents ,例如与data属性,现在它的工作原理与任何html结构, tableul li或没有这些。

 $(document).ready(function() { $('input.child').on('change', function() { var data = $(this).data('name'); if (this.checked) { $('input.parent:checkbox[data-name="' + data + '"]').prop('checked', true); $('input.parent:checkbox[data-parent="' + data + '"]').prop('checked', true); } else { $('input.parent:checkbox[data-name="' + data + '"]').prop('checked', false); $('input.parent:checkbox[data-parent="' + data + '"]').prop('checked', false); } }); }); /* Optional, This is what I added for better look */ $('.child').each(function() { $(this).parent().css({ paddingLeft: 20, backgroundColor: '#c0c0c0' }); }); $('.parent').each(function() { $(this).parent().css({ backgroundColor: 'black' }); }); 
 td { border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table"> <tr> <td> <input type="checkbox" name="checkbox[1]" class="parent" data-name="chck-1" /> </td> <td> </tr> <tr> <td> <input type="checkbox" name="checkbox[2]" class="child" data-name="chck-1" /> </td> <td> </tr> <tr> <td> <input type="checkbox" name="checkbox[3]" class="child" data-name="chck-1" /> </td> <td> </tr> <tr> <td> <input type="checkbox" name="checkbox[4]" class="parent" data-name="chck-2" /> </td> <td> </tr> <tr> <td> <input type="checkbox" name="checkbox[5]" class="child parent" data-name="chck-2" data-parent="chck-3" /> </td> <td> </tr> <tr> <td> <input type="checkbox" name="checkbox[6]" class="child" data-name="chck-3" /> </td> <td> </tr> <tr> <td> <input type="checkbox" name="checkbox[7]" class="child" data-name="chck-3" /> </td> <td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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