使用父ID - jQuery选择所有子复选框

Gol*_*arl -1 html javascript jquery

当我点击parent 1 check all按钮时,应根据父ID选择所有孩子.我有更多的父母,每个父母都有个人身份证.我该怎么做.我尝试了一些不起作用的代码.有人帮我这个.谢谢.

<div>
  <input type="checkbox" id="choice_5621_1"> Parent 1 check all
  <br />
  <input type="checkbox" id="choice_5621_1_1"> <br />
  <input type="checkbox" id="choice_5621_1_2"> <br />
  <input type="checkbox" id="choice_5621_1_3">
</div>
<br /> <br />
<div>
  <input type="checkbox" id="choice_5621_2"> Parent 2 check all <br />
  <input type="checkbox" id="choice_5621_2_1"> <br />
  <input type="checkbox" id="choice_5621_2_2"> <br />
  <input type="checkbox" id="choice_5621_2_3">
</div>
Run Code Online (Sandbox Code Playgroud)

Pet*_*ete 7

当您将所有输入包装在div中时,您可以使用 .nextAll()

// I gave the parent a checkall class just for demo cos not sure what your initial selector is:
$('.checkall').on('change', function() {
   $(this).nextAll('input').prop('checked', this.checked);
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><input type="checkbox" id="choice_5621_1" class="checkall"> Parent 1 check all
  <br />
  <input type="checkbox" id="choice_5621_1_1"> <br />
  <input type="checkbox" id="choice_5621_1_2"> <br />
  <input type="checkbox" id="choice_5621_1_3">
</div>
Run Code Online (Sandbox Code Playgroud)