我在删除表中的 tr 时遇到问题,我需要通过 ajax 填充表,我使用 id 创建 tr 并调用一个调用 ajax 来检索 tds 标签的函数。我有两种情况:响应等于 0 和响应不同于 0。 我的脚本:
<script>
function get_tr(id,name,lastname){
$.ajax({
type: 'POST',
url: '../ajax.php',
data: 'id=' + id + '&name=' + name + '&lastname=' + lastname,
success: function(response){
if(response == 0){
$(this).closest('tr').remove();
}else if(response != 0){
document.getElementById(id).innerHTML = response;
}
}
});
}
</script>
这是我填充表格的循环
<table id="tbl1" class="datatable" border="0" cellpadding="3" cellspacing="1" width="100%">
<tr>
<th width="15%">Name</th>
<th width="15%">Lastname</th>
</tr>
<?php
$queryresult = mysql_query("SELECT id,name,lastname FROM tbl");
while ($data = mysql_fetch_object($queryresult))
{
$id = $data->id;
$name = $data->name;
$lastname = $data->lastname;
?>
<tr id="<?php echo $id; ?>" >
<script>get_tr(<?php echo $id; ?>, <?php echo $name; ?>, <?php echo $lastname; ?> );</script>
</tr>
<?php } ?>
</table>
问题出在脚本中:
$(this).closest('tr').remove();
当我想删除带有响应的 tr 时:0 它不起作用
最佳答案
您不能在成功函数中使用this。为什么您不想在 get_tr
函数中使用简单的 $('#'+id).remove()
?
关于javascript - 通过ajax调用删除表中的tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076597/