首页 > 解决方案 > 带有变量的jQuery中的Concat选择器

问题描述

我想通过其 id 删除表中的每一行。但是,如何将行选择器与变量连接起来?这是 jQuery 代码:

$(document).ready(function() {
  $('.btn-tambah').click(function() {
    var html = $(this).closest("tr").clone().find('td:last').after("<td id='action'><button class='btn btn-danger btn-hapus'>Hapus</button></td>").remove().end().prop('outerHTML');
    $("#daftar-buku-dipinjam").append(html);

    $('tr').each(function() {
      var id = $(this).index() + 1;
      $(this).attr("id", id);
      $('.btn-hapus').click(function() {
        $('#daftar-buku-dipinjam tr td #' + id).parent().remove(); // how to concatenate the selector?
      });
    });

    $('tr td').each(function() {
      $(this).attr("name", "bukudipinjam[]");
    });

    $('tr #action').each(function() {
      $(this).removeAttr("name");
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped table-bordered" id="daftar-buku-dipinjam">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Judul</th>
      <th scope="col">Penulis</th>
      <th scope="col">Action</th>
    </tr>
  </thead>
  <tbody>
    <!--
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td> <a href="">Hapus</a> </td>
    </tr>
    -->
  </tbody>
</table>

标签: javascriptjquery

解决方案


推荐阅读