jQuery深入遍历多个图层以设置检查值

时间:2018-05-06 03:11:11

标签: javascript jquery dom html-table jquery-traversing

如何使用jQuery遍历表格中的这个元素?

#adminreview> thead> tr> th:nth-​​child(1)> div> div> div.checkbox-container> div:nth-​​child(1)>输入

并将输入的checked属性设置为false?

我在我的桌子上使用这个漂亮的过滤器/排序jQuery插件(类似Excel的Bootstrap表排序和过滤插件)。在加载时,我试图只检查其中一个选项,Pending,并运行jQuery来隐藏Approved&单独拒绝行。

在加载时,默认情况下当前检查所有状态:

On Load dropdown selected

目标是仅在加载时检查Pending:

desired options selected on load

这是我的表格html。我知道这与遍历DOM和设置Select All,Approved和&的选中值有关。否认是假的,但我运气不好。

我认为我必须编辑div:nth-child(1)(2)& (3)并使用.prop('checked', false);只是不确定如何使用jquery深入。

HTML(我没有故意包括整个表格):

<table id="adminreview" class="fbody">
  <thead>
    <tr>
      <th class="apply-filter no-search">Status
      <div class="dropdown-filter-dropdown">
        <div class="dropdown-filter-content" style="display: none;">
          <div class="dropdown-filter-sort">
            <span class="a-to-z" data-column="0" data-index="0">A to Z</span>
          </div>
          <div class="dropdown-filter-sort">
            <span class="z-to-a" data-column="0" data-index="0">Z to A</span>
          </div>
          <div class="checkbox-container">
            <div class="dropdown-filter-item">
            <input type="checkbox" value="Select All" checked="checked" class="dropdown-filter-menu-item select-all"
            data-column="0" data-index="0" /> Select All</div>
            <div class="dropdown-filter-item">
            <input type="checkbox" value="Approved" checked="checked" class="dropdown-filter-menu-item item" data-column="0"
            data-index="0" /> Approved</div>
            <div class="dropdown-filter-item">
            <input type="checkbox" value="Denied" checked="checked" class="dropdown-filter-menu-item item" data-column="0"
            data-index="0" /> Denied</div>
            <div class="dropdown-filter-item">
            <input type="checkbox" value="Pending" checked="checked" class="dropdown-filter-menu-item item" data-column="0"
            data-index="0" /> Pending</div>
          </div>
        </div>
      </div></th>          
      <th class="apply-filter">Company
      <div class="dropdown-filter-dropdown">
        <div class="dropdown-filter-content" style="display: none;">
          <div class="dropdown-filter-sort">
            <span class="a-to-z" data-column="1" data-index="1">A to Z</span>
          </div>
          <div class="dropdown-filter-sort">
            <span class="z-to-a" data-column="1" data-index="1">Z to A</span>
          </div>
          <div class="dropdown-filter-search">
            <input type="text" class="dropdown-filter-menu-search form-control" data-column="1" data-index="1"
            placeholder="Search" />
          </div>
          <div class="checkbox-container">
            <div class="dropdown-filter-item">
            <input type="checkbox" value="Select All" checked="checked" class="dropdown-filter-menu-item select-all"
            data-column="1" data-index="1" /> Select All</div>
            <div class="dropdown-filter-item">
            <input type="checkbox" value="ACME Construction Company" checked="checked" class="dropdown-filter-menu-item item"
            data-column="1" data-index="1" /> ACME Construction Company</div>
            <div class="dropdown-filter-item">
            <input type="checkbox" value="Joe&#39;s Concrete" checked="checked" class="dropdown-filter-menu-item item"
            data-column="1" data-index="1" /> Joe&#39;s Concrete</div>
          </div>
        </div>
      </div></th>

还有其他列,我不想触及同一个类: other columns

提前致谢!

1 个答案:

答案 0 :(得分:0)

您在每个复选框上都有item课程。所以你可以循环遍历所有节点,并为所有节点设置checkedfalse。然后。将checked设置为true以获取最后一个。

$('.item').each(function() {
  $( this ).prop('checked', false);
});
$('.item').last().prop('checked', true);