Select 所有没有 类 的复选框

Select all checkboxes without classes

在选中“.selectall”时,我需要select所有复选框,但是在尝试了一系列不同的排列selector的方法之后,我仍然无法得到它工作。我想我不确定如何安排 select 或者说这个 -> 标签 -> 输入。有任何想法吗?谢谢。

$(document).ready(function() {
  $('#selecctall').click(function(event) { //on click 
    if (this.checked) { // check select status
      $('.iw_notify_heirarchical_list li').each(function() { //loop through each checkbox
        $('input', this).checked = true; //select all checkboxes with class "checkbox1"   
      });
    }
  });
});
<ul class="iw_notify_heirarchical_list">
  <label>
    <input type="checkbox" id="selectall">All</label>

  <li id="notifications-164">
    <label class="selectit">
      <input value="164" type="checkbox" name="tax_input[notifications][]" id="in-notifications-164" checked="checked">Calendar Events</label>
    <ul class="children">

      <li id="notifications-173" class="popular-category">
        <label class="selectit">
          <input value="173" type="checkbox" name="tax_input[notifications][]" id="in-notifications-173">Boards and Committees</label>
      </li>

      <li id="notifications-169" class="popular-category">
        <label class="selectit">
          <input value="169" type="checkbox" name="tax_input[notifications][]" id="in-notifications-169">City Council</label>
      </li>

      <li id="notifications-171" class="popular-category">
        <label class="selectit">
          <input value="171" type="checkbox" name="tax_input[notifications][]" id="in-notifications-171" checked="checked">City Events</label>
      </li>

      <li id="notifications-172">
        <label class="selectit">
          <input value="172" type="checkbox" name="tax_input[notifications][]" id="in-notifications-172">Community Events</label>
      </li>

      <li id="notifications-170" class="popular-category">
        <label class="selectit">
          <input value="170" type="checkbox" name="tax_input[notifications][]" id="in-notifications-170">Departments</label>
        <ul class="children">

          <li id="notifications-174">
            <label class="selectit">
              <input value="174" type="checkbox" name="tax_input[notifications][]" id="in-notifications-174" checked="checked">Executive</label>
          </li>

          <li id="notifications-175">
            <label class="selectit">
              <input value="175" type="checkbox" name="tax_input[notifications][]" id="in-notifications-175">Finance</label>
          </li>

          <li id="notifications-176">
            <label class="selectit">
              <input value="176" type="checkbox" name="tax_input[notifications][]" id="in-notifications-176">Fire</label>
          </li>
        </ul>
      </li>
    </ul>
  </li>

  <li id="notifications-165">
    <label class="selectit">
      <input value="165" type="checkbox" name="tax_input[notifications][]" id="in-notifications-165">Employment Opportunities</label>
  </li>

  <li id="notifications-163" class="popular-category">
    <label class="selectit">
      <input value="163" type="checkbox" name="tax_input[notifications][]" id="in-notifications-163" checked="checked">News</label>
  </li>

  <li id="notifications-168">
    <label class="selectit">
      <input value="168" type="checkbox" name="tax_input[notifications][]" id="in-notifications-168">None</label>
  </li>

  <li id="notifications-166">
    <label class="selectit">
      <input value="166" type="checkbox" name="tax_input[notifications][]" id="in-notifications-166">Weekly Calendar Events</label>
  </li>
</ul>

您没有使用正确的 jQuery 语法来选中复选框。

$('input', this).checked = true;

应该是

$('input', this).prop('checked', true);

checked 是 DOM 元素 属性,而不是 jQuery 属性.

您也不需要 .each,因为可以在集合上调用 jQuery 方法,它们将更新所有选定的元素。所以你可以简单地写:

$('.iw_notify_heirarchical_list li :checkbox').prop('checked', true);