javascript - 删除成功时从表中删除行?
问题描述
我想从表中删除已删除的行,但它不适用于我。我尝试了以下代码:
场景:当用户单击删除链接/按钮时,它会发送删除请求并从数据库中删除数据,因此它应该更新表前端视图并在成功时删除单击的删除行。
// Delete remedy which is linked with a Symptom ( Symptoms Table )
$("a.remedy-row-delete").click(function(e) {
e.preventDefault();
var remedy_id = $(this).attr('data-id');
var dataString = '&id='+remedy_id;
$.SmartMessageBox({
title: "Delete Remedy",
content: "Remedy Entry will be deleted, are you sure ?",
buttons: "[YES][NO]"
}, function (ButtonPress) {
if (ButtonPress === "YES"){
$.ajax({
type: 'POST',
data: dataString,
url: '<?php echo $this->CxHelper->Route('eb-admin-delete-linked-remedy-from-symptom') ?>',
success: function(data) {
$("#deleteMessage").show().delay(5000).fadeOut();
$(this).closest('tr').remove();
}
});
}
else {
$("a.remedy-row-delete").removeClass('alert');
}
});
});
我也尝试过$(this).parent().closest('tr').remove();
成功但没有工作。
HTML 标记:
<table id="cx-records-table" class="table display table-striped table-bordered" width="100%">
<thead>
<tr>
<th>
Title
</th>
<th>
Delete
</th>
</tr>
<?php foreach($remedies as $key => $remedy){ ?>
<tr>
<td class="all"><?php echo $remedy['title']; ?><br></td>
<td><a class="cx-action remedy-row-delete" href="javascript:void(0)" data-id="{{remedy['id']}}"><i class="fa fa-trash-o"></i></a></td>
</tr>
<?php } ?>
</thead>
<tbody></tbody>
</table>
谢谢
解决方案
因为函数$(this)
内部ajax
不同于外部,所以你应该做这样的事情
$("a.remedy-row-delete").click(function(e) {
e.preventDefault();
var remedy_id = $(this).attr('data-id');
var dataString = '&id='+remedy_id;
var $this = $(this) // <--- 1. Add this line
$.SmartMessageBox({
...
}, function (ButtonPress) {
if (ButtonPress === "YES"){
$.ajax({
...
success: function(data) {
...
$this.closest('tr').remove(); // <----change this and will works well
}
});
}
else {
$("a.remedy-row-delete").removeClass('alert');
}
});
});
推荐阅读
- python - 如何重塑测试数据框,使维度与训练和预测工作中使用的维度相同?
- python - 使用 Imagemagik 将未嵌入字体的 pdf 转换为 png
- android - 如何在 OpenGL 图像查看器中跟踪“亮点”
- react-native - 尝试注册 RCTBridgeModule 类 RCTWebSocketModule
- python - 我的dqn不好用:奖励不变,损失不断增加
- mysql - MySql 不使用空间索引
- xero-api - 我刚刚更新了我的 Xero 私有应用程序的 Oauth 安全证书,现在我收到一个“错误:1Curl 错误:”
- javascript - 网站的响应式导航栏问题
- css - 如何在 iPad 中将 768×1024 屏幕视图强制为 360×640 视图
- python - 如何在django的views.py中创建一个循环