我在删除表中的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();
当我想删除带有响应:0的tr时,它不起作用
您不能在成功函数中使用这个。为什么不想在get_tr
函数中使用简单的$('#'+id).remove()
?