jQuery 基本 : How can i remove a table row when a button of this row is clicked?

标签 jquery

$("#tableid tbody:last").append(html);

这会动态创建表行。 每个新创建的行都有一个“删除”按钮。

现在,如果我单击删除按钮,该行将被删除。 我怎样才能做到这一点。

提前致谢。

最佳答案

 $(buttonSelector).live ('click', function ()
 {
    $(this).closest ('tr').remove ();
 }
 );

使用 .live 绑定(bind)事件将在动态添加行时自动绑定(bind)它。

编辑

live 现已弃用,我认为是从 1.7 版本开始。

现在的方法是使用 on 而不是 live

$('#tableid').on('click', buttonSelector, function(){
    $(this).closest ('tr').remove ();
});

请参阅doc .

关于jQuery 基本 : How can i remove a table row when a button of this row is clicked?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6831394/

相关文章:

javascript - 如何在数据表中添加行?

javascript - 在 Javascript 中使用 JSON 值

javascript - click 函数中的 var 变得未定义

php - 如何使用ajax在模态弹出窗口中显示php

javascript - jQuery .next 无法运行

jquery - 拖放和排序 jquery UI

javascript - 从多个表单中获取选择框的值

javascript - jQuery 在数据值字符串中动态添加连字符

javascript - 解析错误堆栈

jquery - 以父类为目标并向其添加类