用jQuery删除表行最好的方法是什么?
当前回答
如果您正在使用引导表
将此代码片段添加到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});
本文致谢
其他回答
$('tr').click(function()
{
$(this).remove();
});
我认为你会尝试上面的代码,因为它工作,但我不知道为什么它工作了一段时间,然后整个表被删除。我还试图通过单击该行删除该行。但找不到确切的答案。
您所要做的就是从表中删除表row (<tr>)标记。例如,下面是从表中删除最后一行的代码:
$ (' # myTable tr:去年').remove ();
*以上代码摘自jQuery Howto帖子。
你是对的:
$('#myTableRow').remove();
如果你的行有一个id,这可以很好地工作,比如:
<tr id="myTableRow"><td>blah</td></tr>
如果你没有id,你可以使用jQuery的任何一个选择器。
简单. .试试这个
$("table td img.delete").click(function () {
$(this).parent().parent().parent().fadeTo(400, 0, function () {
$(this).remove();
});
return false;
});
你可以使用:
$($(this).closest("tr"))
用于查找元素的父表行。
它比parent().parent()更优雅,这是我开始做的事情,很快就认识到我的错误。
——编辑 有人指出,这个问题是关于去除这一行的……
$($(this).closest("tr")).remove()
正如下面指出的,你可以简单地做:
$(this).closest('tr').remove();
类似的代码片段可用于许多操作,例如在多个元素上触发事件。