如何使用jQuery在一列中查找过滤器的信息?

时间:2013-10-22 21:29:43

标签: javascript jquery html filter

我使用以下代码过滤表格中的数据:

function searchFilter(ftr,table){
    ftr = '#'+ftr;
    table = '#'+table;
    var $rows = $(table+' tbody tr');
    $(ftr).change(function() {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

        $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
        }).hide();
    });

}

但如果在其他栏目中重复出现信息,它也会显示出来。 如何具体说明我希望过滤器工作的列?假设此列的ID为betaDate。 我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

那么你会得到列的索引

var searchIndex = $("#betaDate").index();

,在过滤行中,您可以使用索引

$(this).find("td").eq(searchIndex).text()...

另一种做法[逻辑可能会关闭,但基本想法]

var index = 1; //starts at one not zero!
var text = "1";
$("table tbody tr td:nth-child(" + index + ")")
    .filter( function() {
        return $(this).text()!==text; 
     })
     .parent()
         .addClass("hide");

fiddle