javascript - 带有变量的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>
解决方案
推荐阅读
- python - pysftp.Connection.get IOError:b35d5d74-ecb1-4a41-a9ed-5dde9b15960d
- flutter - 使用 Flutter 和 Dart,这是实现这个 Future 的最佳方式吗?
- sql-server - 为什么我的总数没有正确显示,我该如何解决?
- sql - 如何在 Ruby on Rails 迁移中修复具有“间隙”的列的顺序
- php - 使用所有可能的属性从字符串中完全删除 img 标签
- machine-learning - 用我自己的训练示例训练 spaCy 现有的词性标注器
- sql - 如何在行而不是列中获取连接表的相邻列?
- python - 有没有办法将位图转换为指令,或者轻松阅读?
- java - 如何在进行谷歌登录时获取用户的年龄和位置
- android - 如何防止 Android 设备在我的应用中显示尺寸缩放?