【发布时间】:2010-09-15 07:41:33
【问题描述】:
用 jQuery 删除表格行的最佳方法是什么?
【问题讨论】:
标签: javascript jquery html-table
用 jQuery 删除表格行的最佳方法是什么?
【问题讨论】:
标签: javascript jquery html-table
以下是否可以接受:
$('#myTableRow').remove();
【讨论】:
你是对的:
$('#myTableRow').remove();
如果您的行有id,这很好用,例如:
<tr id="myTableRow"><td>blah</td></tr>
如果你没有id,你可以使用任何jQuery的plethora of selectors。
【讨论】:
unique 你可以通过为 php <tr id="myTableRow<?php echo $id; ?>"> 附加一些独特的东西来做到这一点,并且在删除它时你必须生成这个 id :)
function removeRow(row) {
$(row).remove();
}
<tr onmousedown="removeRow(this)"><td>Foo</td></tr>
也许这样的事情也能奏效?我没有尝试用“this”做点什么,所以我不知道它是否有效。
【讨论】:
$('#myTable tr').click(function(){
$(this).remove();
return false;
});
一个更好的
$("#MyTable").on("click", "#DeleteButton", function() {
$(this).closest("tr").remove();
});
【讨论】:
$('tr').click(function()
{
$(this).remove();
});
我想你会尝试上面的代码,因为它可以工作,但我不知道为什么它会工作一段时间,然后整个表都被删除了。我也试图通过单击该行来删除该行。但找不到确切的答案。
【讨论】:
假设您在表格的数据单元格中有一个按钮/链接,这样的事情就可以解决问题...
$(".delete").live('click', function(event) {
$(this).parent().parent().remove();
});
这将删除所单击按钮/链接的父级的父级。您需要使用 parent() 因为它是一个 jQuery 对象,而不是普通的 DOM 对象,并且您需要使用 parent() 两次,因为按钮位于数据单元格内,该单元格位于一行内......这是您要删除的内容。 $(this) 是被点击的按钮,所以只要有这样的东西只会删除按钮:
$(this).remove();
虽然这将删除数据单元格:
$(this).parent().remove();
如果您想简单地单击该行上的任意位置以将其删除,则可以使用类似这样的方法。您可以轻松修改它以提示用户或仅在双击时工作:
$(".delete").live('click', function(event) {
$(this).parent().remove();
});
希望对您有所帮助...我自己在这方面有点挣扎。
【讨论】:
您所要做的就是从表格中删除表格行 (<tr>) 标记。例如这里是从表中删除最后一行的代码:
$('#myTable tr:last').remove();
*以上代码取自this jQuery Howto post。
【讨论】:
你可以使用:
$($(this).closest("tr"))
用于查找元素的父表行。
它比 parent().parent() 更优雅,这是我开始做的,很快就发现了我的方法的错误。
--编辑-- 有人指出问题是关于删除行...
$($(this).closest("tr")).remove()
如下所述,您可以简单地这样做:
$(this).closest('tr').remove();
类似的代码 sn-p 可用于许多操作,例如在多个元素上触发事件。
【讨论】:
$(this).closest("tr").remove()
简单..试试这个
$("table td img.delete").click(function () {
$(this).parent().parent().parent().fadeTo(400, 0, function () {
$(this).remove();
});
return false;
});
【讨论】:
$(this).parent().parent().parent() 更改为$(this).closest('tr')。它更强大,更清楚地显示了您正在选择的内容。
试试这个尺寸
$(this).parents('tr').first().remove();
完整列表:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.deleteRowButton').click(DeleteRow);
});
function DeleteRow()
{
$(this).parents('tr').first().remove();
}
</script>
</head>
<body>
<table>
<tr><td>foo</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
<tr><td>bar bar</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
<tr><td>bazmati</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
</table>
</body>
</html>
【讨论】:
如果您要删除的行可能会更改,您可以使用它。只需将此函数传递给您要删除的行#。
function removeMyRow(docRowCount){
$('table tr').eq(docRowCount).remove();
}
【讨论】:
如果你有这样的 HTML
<tr>
<td><span class="spanUser" userid="123"></span></td>
<td><span class="spanUser" userid="123"></span></td>
</tr>
其中userid="123" 是您可以在构建表时动态填充的自定义属性,
你可以使用类似的东西
$(".spanUser").live("click", function () {
var span = $(this);
var userid = $(this).attr('userid');
var currentURL = window.location.protocol + '//' + window.location.host;
var url = currentURL + "/Account/DeleteUser/" + userid;
$.post(url, function (data) {
if (data) {
var tdTAG = span.parent(); // GET PARENT OF SPAN TAG
var trTAG = tdTAG.parent(); // GET PARENT OF TD TAG
trTAG.remove(); // DELETE TR TAG == DELETE AN ENTIRE TABLE ROW
} else {
alert('Sorry, there is some error.');
}
});
});
因此,在这种情况下,您不知道 TR 标记的类或 ID,但无论如何您都可以将其删除。
【讨论】:
empty() 的另一个:
$(this).closest('tr').empty();
【讨论】:
我很欣赏这是一篇旧帖子,但我也想做同样的事情,但发现接受的答案对我不起作用。假设 JQuery 自从写完之后就继续前进了。
无论如何,我发现以下内容对我有用:
$('#resultstbl tr[id=nameoftr]').remove();
不确定这是否对任何人有帮助。我上面的示例是一个更大函数的一部分,因此没有将其包装在事件侦听器中。
【讨论】:
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>
你可以使用一个函数来选择这样的行(ES6):
const rowRemover = (category,type)=>{
$(`tr[category=${category}][type=${type}]`).remove();
}
rowRemover('petshop','fish');
【讨论】:
如果您使用的是引导表
将此代码 sn-p 添加到您的 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});
【讨论】:
从表中删除行的最简单方法:
例如:
<table id='myTable' border='1'>
<tr id='tr1'><td>Row1</td></tr>
<tr id='tr2'><td>Row2</td></tr>
<tr id='tr3'><td>Row3</td></tr>
<tr id='tr4'><td>Row4</td></tr>
<tr id='tr5'><td>Row5</td></tr>
</table>
//======REMOVE TABLE ROW=========
//1. remove spesific row using its ID
$('#tr1').remove();
//2. remove spesific row using its order or index.
//row index started from 0-n. Row1 index is 0, Row2 index is 1 and so on.
$('#myTable').find('tr:eq(2)').remove();//removing Row3
【讨论】: