【问题标题】:JQuery table search number of rows by multiple contains valueJQuery表按多个包含值搜索行数
【发布时间】:2016-08-22 21:01:21
【问题描述】:

我想在表格中按多个 :contains() 的 td 值搜索行数。

<table id="tabCity">
<tr><td>Name</td><td>Surname</td><td>City</td></tr>
<tr><td>Paul</td><td color>Smith</td><td>Boston</td></tr>
<tr><td>Andrew</td><td>Smith</td><td>London</td></tr>
<tr><td>Smith</td><td>Green</td><td>Boston</td></tr>
<tr><td>Nick</td><td>Smith</td><td>Boston</td></tr>
</table>
<br>
<button class="go">Search Surname Smith in Boston</button>
<br>
<label>The result must be 2</label>

$(document).on('click', '.go', function(){
var rows= $("#tabCity tr td:nth-child(2):contains('Smith'), td:nth-child(3):contains('Boston')");
alert(rows.length);
});

看这里:http://jsfiddle.net/drh0mvhz/1

【问题讨论】:

    标签: jquery pseudo-class


    【解决方案1】:

    .filter() 允许您进行更复杂的过滤:

    var rows = $("#tabCity tr").filter(function() {
        return $(this).find("td:nth-child(2):contains('Smith')").length 
            && $(this).find("td:nth-child(3):contains('Boston')").length;
    });
    

    http://jsfiddle.net/bh6t6tf6/

    【讨论】:

    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多