2 回答
TA贡献1793条经验 获得超6个赞
您可以使用$(this).closest('tr').remove()这里的示例
$(".delete").on("click",function(){
$(this).closest('tr').remove();
})
.pointer{
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<table class="table table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
<tr>
<td scope="col">1</td>
<td scope="col">1</td>
<td scope="col">1</td>
<td scope="col">Edit</td>
<td class="delete pointer" scope="col">Delete</td>
</tr>
<tr>
<td scope="col">2</td>
<td scope="col">2</td>
<td scope="col">2</td>
<td scope="col">Edit</td>
<td class="delete pointer" scope="col">Delete</td>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
TA贡献1850条经验 获得超11个赞
您想要在创建删除按钮的位置添加删除按钮的 onClick 处理程序。这样您就可以使用“this-context”,这使得处理亲戚父母或后代变得更容易。这是您的代码的简化示例,用于演示:
$("#btnAdd").on('click', function() {
if(
$("#insert-image").val() !== '' &&
$("#insert-name").val() !== '' &&
$("#insert-surname").val() !== ''
){
let row = '<tr><td>image</td><td>name</td><td>surname</td><td>edit</td><td>delete</td></tr>'
$('tbody').append(row);
$('td:contains("edit")').html('<button>edit</button')
$('td:contains("delete")').html('<button>delete</button>')
//Add your eventhandler here so u can use the parent of "this" button $(this).parent
.on('click', function() {
$(this).parents('tr').remove();
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<button id="btnAdd">Add</button>
- 2 回答
- 0 关注
- 146 浏览
添加回答
举报