在JQuery中,使用以下代码可轻松隐藏基于预定义列td值的表行。
function filterRows(word){
$('.application>tbody>tr')
.show()
.find('td:nth-child(2)').not(':contains("'+word+'")')
.parent()
.hide()
}
Run Code Online (Sandbox Code Playgroud)
但是,我将如何在多列中显示与td值匹配的行。
类似于以下内容(无效)
function filterRows(word){
$('.application>tbody>tr')
.show()
.find('td:nth-child(2)').not(':contains("'+word+'")')
.find('td:nth-child(3)').not(':contains(30)')
.parent()
.hide()
}
Run Code Online (Sandbox Code Playgroud)
基本上,我只希望在第二列td中仅显示我的单词在“ word”中传递的行,而第三列包含“ 30”。
感谢您的注意。
您可以.end()像这样在链中跳回去:
function filterRows(word){
$('.application>tbody>tr').show()
.find('td:nth-child(2)').not(':contains("'+word+'")').parent().hide()
.end().end().end() //back 3 spots for parent, not and find
.find('td:nth-child(3)').not(':contains(30)').parent().hide();
}
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,链接有点冗长,只是将引用保留在变量中,如下所示:
function filterRows(word){
var rows = $('.application>tbody>tr').show();
rows.find('td:nth-child(2):not(:contains("'+word+'"))').parent().hide();
rows.find('td:nth-child(3):not(:contains(30))').parent().hide();
}
Run Code Online (Sandbox Code Playgroud)
或更复杂的选择器:
function filterRows(word){
$('.application>tbody>tr').show()
.find('td:nth-child(2):not(:contains("'+word+'")), td:nth-child(3):not(:contains(30))').parent().hide();
}
Run Code Online (Sandbox Code Playgroud)