jQuery basic:如何在单击此行的按钮时删除表行?
$("#tableid tbody:last").append(html);
这会动态创建表行。 每个新创建的行都有一个“删除”按钮。
现在,如果我单击该删除按钮,该行将被删除。 我怎样才能做到这一点。
提前致谢。
$(buttonSelector).live ('click', function () { $(this).closest ('tr').remove (); } );
使用.live
绑定事件会在动态添加行时自动绑定它。
编辑
live
我现在已弃用,因为我认为1.7版本。
现在的方法是使用而不是live
。
$('#tableid').on('click', buttonSelector, function(){ $(this).closest ('tr').remove (); });
见文档 。
您可以使用此代码删除包含单击按钮的父行:
$(myButtonSelector).click(function(){ $(this).parents('tr').first().remove(); });
有关实例,请参阅此链接 。
有关更多信息,请参阅此文章 。
你可以这样做:
$('.add').click(function(){ $("#tableid tbody:last").append('Hi Remove'); }); $('.remove').live('click',function(){console.log($(this).parent().parent().remove())});