搜索动态创建的行并删除

search dynamically created row and delete

我正在尝试搜索由另一个函数动态创建的具有 userName 的特定行并将其从 table.But 中删除我现在可以搜索该行了。
这就是我添加新行的方式:

$("table").append("<tr><td>" + userName + "</td></tr>");

这就是我在 table 中搜索具有 userName 的行并将其删除的方式:

$("table").find("<tr><td>" + userName + "</td></tr>").remove();

find 的参数是一个选择器,而不是 HTML。

$("table").find("td:contains("+userName+")").remove();

您可以在选择器中使用 contains() 函数,请参见下面的示例代码

$("table").find("tr > td:contains('"+ userName +"')").remove();

使用基于 contains() 的选择器

$('table td:contains('+userName+')').remove()

$('table').find('td:contains('+userName+')').remove()

Fiddle

您的代码不是查找现有 DOM children.

的正确方法

对于完全匹配使用 .filter(),

$("table td").filter(function() {
    return $(this).text() === userName;
}).remove()

即使你尝试 jQuery 最近。

$('table').closest('tr').remove();