如果row包含string,则删除整行

时间:2014-06-18 02:13:49

标签: jquery regex html-table removechild

正如标题所说,如果匹配发生,我正试图删除表格的整行。我有以下内容:

$('tr > td:nth-child(3)').text().match(/\$/g);

所以在这种情况下,如果第三列包含'$',我希望删除整行。我不知道如何解决这个问题,因为上面只返回一个长度匹配的数组,每个索引都是匹配的字符串。

伪:

if($('tr > td:nth-child(3)').text().match(/\$/g)){
    $(this).remove();
}

我整天都在编程,所以我的大脑都被炒了,我确信有一些非常简单的东西我会忽略。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:3)

我相信你可以使用:contains,在这种情况下:

$('tr > td:nth-child(3):contains("$")').remove();

答案 1 :(得分:3)

建议使用null的细微差别将删除整行:

$('tr > td:nth-child(3):contains("$")').parent().remove();

jsFiddle

答案 2 :(得分:0)

match方法返回一个数组。您想要使用test方法。

$('tr').filter(function() {
    return /\$/.test( $(this).find('td:nth-child(3)').text() );
})
.remove();