用jQuery删除表行最好的方法是什么?
当前回答
简单. .试试这个
$("table td img.delete").click(function () {
$(this).parent().parent().parent().fadeTo(400, 0, function () {
$(this).remove();
});
return false;
});
其他回答
$('tr').click(function()
{
$(this).remove();
});
我认为你会尝试上面的代码,因为它工作,但我不知道为什么它工作了一段时间,然后整个表被删除。我还试图通过单击该行删除该行。但找不到确切的答案。
$('#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});
本文致谢
Id现在不是一个好的选择器。您可以在行上定义一些属性。你可以用它们作为选择器。
<tr category="petshop" type="fish"><td>little fish</td></tr>
<tr category="petshop" type="dog"><td>little dog</td></tr>
<tr category="toys" type="lego"><td>lego starwars</td></tr>
你可以使用func来选择行,就像这样(ES6):
const rowRemover = (category,type)=>{
$(`tr[category=${category}][type=${type}]`).remove();
}
rowRemover('petshop','fish');
function removeRow(row) {
$(row).remove();
}
<tr onmousedown="removeRow(this)"><td>Foo</td></tr>
也许像这样的东西也能起作用?我还没有尝试过用“this”做什么,所以我不知道它是否有效。