我有一个显示用户数据的表。每行可以有2个功能,一个用于删除,另一个用于编辑记录。行本身有一个我需要为删除和编辑功能引用的id。我很难让它正常工作。
右侧的最后一个单元格用于删除图像。当我单击删除图标时,即使我在not()
中指定了删除单元格,也会从该行收到警报。
我遇到的最后一个问题是$.post
没有传递任何POST值。即使我手动设置,我也看不到服务器端。
到目前为止,这是我的代码。
$('#drafts').find('tr').not(':first-child, td.delete').click( function(){
alert(this.id);
});
$("td.delete").click(function(){
var id = this.id;
$.post('test.php',id, function(){
$('#'+id).remove();
},'json');
});
<tr id="5">
<td>test1</td>
<td>test2</td>
<td class="delete"> </td>
</tr>
答案 0 :(得分:2)
使用此代码:
$("td.delete").click(function(){
var id = $(this).parents('tr').eq(0).attr('id');
$.post('test.php', {'id': id}, function(){
$('#'+id).remove();
},'json');
});
这个用于 edit
操作(但我不知道你想在这里做什么):
$("td.edit").click(function(){
var id = $(this).parents('tr').eq(0).attr('id');
// now you have `id` of current row
// write your code here
});
你说除了edit
单元格之外,你想要delete
动作的整行:
$("tr td:not(.delete)").click(function(){
var id = $(this).parents('tr').eq(0).attr('id');
$.post('test.php', {'id': id}, function(){
$('#'+id).remove();
},'json');
});
答案 1 :(得分:1)
无需使用ID进行删除。只需遍历树到行:
$("td.delete").click(function (){
var $row = $(this).closest('tr');
$.post('test.php',{id: $row.attr('id')}, function(){
$row.remove();
},'json');
});