javascript - jquery 通过点击一个标签删除一行表 (<tr>)

标签 javascript jquery html css jquery-ui

我开发了一个表格,每行有 1 列,每行有一个复选框和 2 个标签。

$('input[type="label"]').click(function() {
      var id = $(this).attr("id");
      $(this).parent("tr:first").remove()
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>To Do List</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <div class="checkbox">
            <input type="checkbox" id="checkbox1" /><label>Task 1</label><label class="glyphicon glyphicon-trash"></label>
          </div>
        </td>
      </tr>

      <tr>
        <td>
          <div class="checkbox">
            <input type="checkbox" id="checkbox2" /><label>Task 2</label><label class="glyphicon glyphicon-trash"></label>
          </div>
        </td>
      </tr>

      <tr>
        <td>
          <div class="checkbox">
            <input type="checkbox" id="checkbox3" /><label>Task 3</label><label class="glyphicon glyphicon-trash" id="3"></label>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</div>

当我点击删除标签时,整行都应该被删除。它下面的行应该自动排在上面一个位置。

应该在 jquery 中做哪些更正?

最佳答案

您需要在具有类 glyphiconglyphicon 的标签上使用 $(this).closest("tr").remove() 和点击事件-垃圾:

$('.glyphicon.glyphicon-trash').click(function(){
      $(this).closest("tr").remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">

  <table class="table">
    <thead>
      <tr>
        <th>To Do List</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <div class="checkbox">
            <input type="checkbox" id="checkbox1" /><label>Task 1</label><label class="glyphicon glyphicon-trash"></label>
          </div>
        </td>
      </tr>

      <tr>
        <td>
          <div class="checkbox">
            <input type="checkbox" id="checkbox2" /><label>Task 2</label><label class="glyphicon glyphicon-trash"></label>
          </div>
        </td>
      </tr>

      <tr>
        <td>
          <div class="checkbox">
            <input type="checkbox" id="checkbox3" /><label>Task 3</label><label class="glyphicon glyphicon-trash" id="3"></label>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</div>

关于javascript - jquery 通过点击一个标签删除一行表 (<tr>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51442885/

相关文章:

javascript - 使用这种 JavaScript 编码模式来定义构造函数有什么好处?

javascript - Unicode 字符无法在 HTML5 Canvas 中正确呈现

javascript - onChange 事件未在 Chrome 中的文本框上触发

javascript - Codemirror Clike 模式似乎不起作用

javascript - 通过 Javascript 设置 HTML 文本字段

javascript - 如何让文本区域在更新时保持滚动到底部

javascript - 什么免费的 javascript 编辑器可以做这种智能感知

Jquery UI 可排序存在

jquery - 验证JQuery : Uncaught TypeError: Cannot call method 'call' of undefined

javascript - 使用 jQuery 在更改事件上创建新的下拉菜单