我有一个为用户显示数据的表。每行可以有2个函数,一个用于删除记录,另一个用于编辑记录。行本身有一个id,我需要在delete和edit函数中引用它。我有一段时间很难让它正常工作。
右边的最后一个单元格用于删除图像。当我只单击删除图标时,即使我在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>
发布于 2013-01-05 13:22:04
使用以下代码:
$("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
});
您说过,除了delete
单元格之外,您想要edit
操作的整个行:
$("tr td:not(.delete)").click(function(){
var id = $(this).parents('tr').eq(0).attr('id');
$.post('test.php', {'id': id}, function(){
$('#'+id).remove();
},'json');
});
发布于 2013-01-05 13:24:05
删除时不需要使用ID。只需一行一行地遍历:
$("td.delete").click(function (){
var $row = $(this).closest('tr');
$.post('test.php',{id: $row.attr('id')}, function(){
$row.remove();
},'json');
});
https://stackoverflow.com/questions/14168845
复制相似问题