0

以下是我的 HTML 代码:

<!-- Following is the code for parent checkbox-->
<input id="ckbCheckAll" class="custom-check ez-hide" type="checkbox" name=""></input>
<!-- Following is the code for child checkboxes-->
<input id="practice_6_191" class="custom-check ez-hide" type="checkbox" onchange="enable_text_boxes('practice_6_191')" value="191" name="practice_topics_6[]"></input>
<input id="practice_6_190" class="custom-check ez-hide" type="checkbox" onchange="enable_text_boxes('practice_6_190')" value="190" name="practice_topics_6[]"></input>
<input id="practice_6_191" class="custom-check ez-hide" type="checkbox" onchange="enable_text_boxes('practice_6_191')" value="191" name="practice_topics_6[]"></input>
<input id="practice_6_192" class="custom-check ez-hide" type="checkbox" onchange="enable_text_boxes('practice_6_192')" value="192" name="practice_topics_6[]"></input>
<input id="practice_6_193" class="custom-check ez-hide" type="checkbox" onchange="enable_text_boxes('practice_6_193')" value="193" name="practice_topics_6[]"></input>
<input id="practice_6_195" class="custom-check ez-hide" type="checkbox" onchange="enable_text_boxes('practice_6_195')" value="195" name="practice_topics_6[]"></input>
<input id="practice_6_196" class="custom-check ez-hide" type="checkbox" onchange="enable_text_boxes('practice_6_196')" value="196" name="practice_topics_6[]"></input>

现在我想创建一个上面的 ids 数组<input type="checkbox">HTML 元素的 id 数组,并在父复选框的单击事件上使用 foreach 循环访问该数组。

也就是说,当父复选框被选中时,所有子复选框也应该被选中,当父复选框被取消选中时,所有子复选框也应该被取消选中。

当用户一个一个地检查所有子复选框时,父复选框也应该被选中,当用户取消选中任何子复选框时,当父复选框被选中时,父复选框也应该被取消选中。

你能在这方面帮助我吗?

另外,更重要的一件事是我不想在类选择器的帮助下访问子复选框,即 class="custom-check" 或 class="ez-hide" 因为这些类对所有子复选框都是通用的。我想创建一个数组,然后在我的代码中访问这个数组。提前致谢。

4

1 回答 1

4

使用.map()

var id_checkboxes = $('input[type="checkbox"]').map(function () {
    return this.id;
}).get();
于 2013-10-17T11:29:03.550 回答