用jQuery删除表行最好的方法是什么?
当前回答
另一个是empty():
$(this).closest('tr').empty();
其他回答
$('#myTable tr').click(function(){
$(this).remove();
return false;
});
甚至是更好的
$("#MyTable").on("click", "#DeleteButton", function() {
$(this).closest("tr").remove();
});
如果您正在使用引导表
将此代码片段添加到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});
本文致谢
你是对的:
$('#myTableRow').remove();
如果你的行有一个id,这可以很好地工作,比如:
<tr id="myTableRow"><td>blah</td></tr>
如果你没有id,你可以使用jQuery的任何一个选择器。
function removeRow(row) {
$(row).remove();
}
<tr onmousedown="removeRow(this)"><td>Foo</td></tr>
也许像这样的东西也能起作用?我还没有尝试过用“this”做什么,所以我不知道它是否有效。
如果要删除的行可能发生变化,可以使用此方法。只需将您希望删除的行#传递给该函数。
function removeMyRow(docRowCount){
$('table tr').eq(docRowCount).remove();
}