【问题标题】:jQuery check the parent checkbox if the child checkbox is checked如果选中子复选框,则jQuery检查父复选框
【发布时间】:2018-03-06 11:17:31
【问题描述】:

我想制定一个逻辑,如果选中子复选框,它也会检查父复选框。输入可以是父级、子级和两者:

<input type="checkbox" name="checkbox[$counter]" class="parent" />
<input type="checkbox" name="checkbox[$counter]" class="child" />
<input type="checkbox" name="checkbox[$counter]" class="parent child" />

这个结构:

  • 1
    • 2
    • 3
  • 4
    • 5
      • 6
      • 7

会是这样的:

<table class="table">
  <tr>
    <td>
      <input type="checkbox" name="checkbox[1]" class="parent" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[2]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[3]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[4]" class="parent" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[5]" class="child parent" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[6]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox[7]" class="child" />
    </td>
    <td>
     .
     .
  </tr>
</table>

如果我检查 3,它会检查 1,如果我检查 6,它也会检查 5 和 4。

复选框也位于单独的表格行中,因为我想在复选框旁边的表格中显示一些结构化的信息。

你能告诉我一个脚本是做什么的吗?我试过了,但它不起作用:

$(document).ready(function() {
    $('input.child').change(function() {
        if ($(this).is(':checked')) {
            $(this).closest('input.parent:checkbox').attr('checked', true);
        }
    });
});

【问题讨论】:

  • 是li ui结构吗?
  • checkbox 不是chechkbox
  • @programtreasures table tr td 结构
  • @Pedram 对不起,错字
  • 您的html structure 很重要,因为这些答案在这里没有任何表格,您需要使用表格结构更新您的问题。

标签: javascript jquery checkbox input


【解决方案1】:

复选框不是父子关系,因此 .closest() 方法将不起作用,因为它从自身开始遍历。

我建议您使用 TR 元素分配类,并为它们附加事件处理程序,遍历 DOM 将很容易。

<tr class="parent">...</tr>
<tr class="child">...</tr>

您可以使用.prevAll().first() 的组合来获得目标TR,然后使用.find() 获得复选框。

另外,如果要触发更改事件,可以使用trigger()方法。

$(document).ready(function() {
  $('.child').on('change', ':checkbox', function() {
    if ($(this).is(':checked')) {
      var currentRow = $(this).closest('tr');
      var targetedRow = currentRow.prevAll('.parent').first();
      var targetedCheckbox = targetedRow.find(':checkbox');
      targetedCheckbox.prop('checked', true).trigger('change');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<table class="table">
  <tr class="parent">
    <td>
      <input type="checkbox" name="checkbox[1]" />
    </td>
  </tr>
  <tr class="child">
    <td>
      <input type="checkbox" name="checkbox[2]" />
    </td>
  </tr>
  <tr class="child">
    <td>
      <input type="checkbox" name="checkbox[3]" />
    </td>
  </tr>
  <tr class="parent">
    <td>
      <input type="checkbox" name="checkbox[4]" />
    </td>
  </tr>
  <tr class="child parent">
    <td>
      <input type="checkbox" name="checkbox[5]" />
    </td>
  </tr>
  <tr class="child">
    <td>
      <input type="checkbox" name="checkbox[6]" />
    </td>
  </tr>
  <tr class="child">
    <td>
      <input type="checkbox" name="checkbox[7]" />
    </td>
  </tr>
</table>

【讨论】:

  • 我使用了您的代码,它在检查子输入时工作正常。我有两个问题,首先是当我取消选中所有子输入时,我希望取消选中父输入,其次是当我取消选中我想要的父输入时可以这样做吗?
【解决方案2】:

试试下面的脚本,

$(document).on('change', 'input[type=checkbox]', function(e) {
  $(this).siblings('ul').find("input[type='checkbox']").prop('checked', this.checked);
  $(this).parentsUntil('input[type=checkbox]').children("input[type='checkbox']").prop('checked', this.checked);
  e.stopPropagation();
});

【讨论】:

    【解决方案3】:

    使用您当前的html 结构,您需要在childsparents 之间建立链接,例如使用data 属性,现在它适用于任何html 结构、tableul li 或没有这些。

    $(document).ready(function() {
      $('input.child').on('change', function() {
        var data = $(this).data('name');
    
        if (this.checked) {
          $('input.parent:checkbox[data-name="' + data + '"]').prop('checked', true);
          $('input.parent:checkbox[data-parent="' + data + '"]').prop('checked', true);
        } else {
          $('input.parent:checkbox[data-name="' + data + '"]').prop('checked', false);
          $('input.parent:checkbox[data-parent="' + data + '"]').prop('checked', false);
        }
      });
    });
    
    /* Optional, This is what I added for better look */
    
    $('.child').each(function() {
      $(this).parent().css({
        paddingLeft: 20,
        backgroundColor: '#c0c0c0'
      });
    });
    
    $('.parent').each(function() {
      $(this).parent().css({
        backgroundColor: 'black'
      });
    });
    td {
      border: 1px solid black;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table class="table">
      <tr>
        <td>
          <input type="checkbox" name="checkbox[1]" class="parent" data-name="chck-1" />
        </td>
        <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="checkbox[2]" class="child" data-name="chck-1" />
        </td>
        <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="checkbox[3]" class="child" data-name="chck-1" />
        </td>
        <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="checkbox[4]" class="parent" data-name="chck-2" />
        </td>
        <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="checkbox[5]" class="child parent" data-name="chck-2" data-parent="chck-3" />
        </td>
        <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="checkbox[6]" class="child" data-name="chck-3" />
        </td>
        <td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="checkbox[7]" class="child" data-name="chck-3" />
        </td>
        <td>
      </tr>
    </table>

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多