php - jQuery:隐藏表格行
问题描述
这是按下按钮删除时调用的函数。确认删除后,我想隐藏删除 tr :
function delete_confirmation(id) {
var $tr = $(this).closest('tr');
swal({
title: "Are you sure?",
text: "To delete this patient!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
$.ajax({
url: localStorage.getItem("base_url") + '' + 'index.php/Patient_ws/delete/',
timeout: 4000,
type: "POST",
data: ({id: id}),
}).done(function () {
$tr.remove();
})
.fail(function () {
swal({
title: ' erreur in server, try later...',
type: 'warning'
})
})
} else {
swal("Patient not deleted");
}
});
}
html标记:
<a href="#" class="btn btn-danger btn-xs" onclick="delete_confirmation(<?php echo $patient->id_personne;?>)">
<i class="fa fa-trash-o"></i> Delete </a>
我想隐藏 tr - 请帮忙。
解决方案
除非您从 html 中传入,否则函数this
中没有显式。onclick
尝试console.log(this)
会看到它可能Window
不是元素
由于您已经在使用 jQuery,我建议您也将它用于事件侦听器并将 php 输出移动到数据属性
HTML
<a class="delete-row-btn btn btn-danger btn-xs"
data-id="<?php echo $patient->id_personne;?>">
JS
$(document).on('click','.delete-row-btn', delete_confirmation)
function delete_confirmation(event) {
event.preventDefault();
// now `this` is the button
var id = $(this).data('id')
var $tr = $(this).closest('tr');
// all else should be same
swal({
.....
}
推荐阅读
- java - 在java中替换字符串的正则表达式
- tensorflow - 无法在 PyCharm 2019.2 中导入 Keras(来自 TensorFlow 2.0)
- angular - 用于传递数据父级的 Observable 和 EventEmitter (Angular)
- python - 将 Python 配置为复杂的 Django 项目
- amazon-web-services - 由 ansible 提供的打包程序在 aws codebuild 中失败
- javascript - 在较大字符串中的特定字符串之前返回第一次出现的时间正则表达式
- c# - 在 Vuforia for Unity 中查找地平面的变换旋转值
- python - 需要修复无法更新 conda all package 的错误吗?
- azure-sql-database - 在更短的时间内将 50GB CSV 文件 Azure Blob 加载到 Azure SQL DB - 性能
- python - Azure Functions 中的多处理