用jQuery删除表行最好的方法是什么?


当前回答

简单. .试试这个

$("table td img.delete").click(function () {
    $(this).parent().parent().parent().fadeTo(400, 0, function () { 
        $(this).remove();
    });
    return false;
});

其他回答

如果您正在使用引导表

将此代码片段添加到bootstrap_table.js中

BootstrapTable.prototype.removeRow = function (params) {
    if (!params.hasOwnProperty('index')) {
        return;
    }

    var len = this.options.data.length;

    if ((params.index > len) || (params.index < 0)){
        return;
    }

    this.options.data.splice(params.index, 1);

    if (len === this.options.data.length) {
        return;
    }

    this.initSearch();
    this.initPagination();
    this.initBody(true);
};

然后在你的var allowedMethods = [

添加“removeRow”

最后你可以使用$("#your-table").bootstrapTable('removeRow',{index:1});

本文致谢

我知道这是一个老帖子,但我也想做同样的事情,发现公认的答案对我不适用。假设JQuery在写这篇文章之后一直在前进。

不管怎样,我发现下面的方法对我很有用:

$('#resultstbl tr[id=nameoftr]').remove();

不知道这对谁有帮助。我上面的例子是一个更大的函数的一部分,所以没有将它包装在事件侦听器中。

以下是可以接受的:

$('#myTableRow').remove();

您所要做的就是从表中删除表row (<tr>)标记。例如,下面是从表中删除最后一行的代码:

$ (' # myTable tr:去年').remove ();

*以上代码摘自jQuery Howto帖子。

如果要删除的行可能发生变化,可以使用此方法。只需将您希望删除的行#传递给该函数。

function removeMyRow(docRowCount){
   $('table tr').eq(docRowCount).remove();
}