如果未经检查的jquery,则删除表行

问题描述 投票:0回答:2

我正在使用一个表,每行包含一个复选框,而我提交我想要未选中的行被禁用这是我试图我创建一个表和行包含输入类型检查输入类型未选中表示我想要禁用该行所以当我在mvc中检索时,我可以得到详细信息

<table id="subjectTable">
<tbody><tr><input type='checkbox' name='subjectcheck' /></tr></tbody>             
</table>
 $("input:not(:checked)").each(function () {
                        $(this).closest('tr').attr("disabled", "true");
                    });

这个我怎么试过可以任何一个帮助

javascript jquery
2个回答
1
投票

如果你想简单地删除<tr>,你可以使用jQuery方法.closest().remove()

  • 使用$('input:not(:checked)')选择输入
  • 找到最近的<tr>.closest('tr')
  • .remove()删除它们

PS:请记住,你必须在你的<td>内有一个<tr>,否则你的<input>将放在你的<table>之外。

$('button').on('click', function(){
  $('input:not(:checked)').closest('tr').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="subjectTable">
  <tbody>
    <tr><td><input type='checkbox' name='subjectcheck' /></td></tr>
  </tbody>
</table>
<button>Emulate submit !</button>

1
投票

要删除未经检查的tr的父tr,请尝试.parent().remove(),如下所示:

$("input:not(:checked)").each(function () {
  $(this).parent('tr').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="subjectTable">
  <tbody>
    <tr>
      <input type='checkbox' name='subjectcheck'/>
    </tr>
  </tbody>             
</table>
© www.soinside.com 2019 - 2024. All rights reserved.