在 JQuery 中,使用以下代码可以轻松基于预定义列 td 值隐藏表行。
function filterRows(word){
$('.application>tbody>tr')
.show()
.find('td:nth-child(2)').not(':contains("'+word+'")')
.parent()
.hide()
}
但是,我将如何显示与多列中的 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()
}
基本上,我希望能够仅显示在“word”中传递我的单词的行位于第二列 td 中,第三列包含“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();
}
不过,在这种情况下,链接有点冗长,只需在变量中保留引用,如下所示:
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();
}
或者更复杂一点的选择器:
function filterRows(word){
$('.application>tbody>tr').show()
.find('td:nth-child(2):not(:contains("'+word+'")), td:nth-child(3):not(:contains(30))').parent().hide();
}
关于jquery - 根据多列中的值显示行。 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4206354/