【问题标题】:How to create an array of all ids having <input type="checkbox"> in jQuery?如何在 jQuery 中创建具有 <input type="checkbox"> 的所有 id 的数组?
【发布时间】:2013-10-25 20:52:31
【问题描述】:

以下是我的 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>

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

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

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

在这方面你能帮我吗?

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

【问题讨论】:

    标签: javascript jquery html checkbox


    【解决方案1】:

    使用.map()

    var id_checkboxes = $('input[type="checkbox"]').map(function () {
        return this.id;
    }).get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-05
      • 2010-12-31
      • 1970-01-01
      • 2011-09-04
      • 2011-02-23
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      相关资源
      最近更新 更多