【问题标题】:Filter First column of table Jquery过滤表Jquery的第一列
【发布时间】:2013-12-14 19:54:44
【问题描述】:

我正在尝试使用 Jquery 为 HTML 表构建一个选择过滤器,并且我只想过滤该表的第一列,到目前为止我已经有了这个功能:

$('#select').change(function() {
    var rows = $("#tablebody_hosts").find("tr").hide();
    var data = this.value.split(" ");
    $.each(data, function(i, v) {
        rows.filter(":contains(" + v + ")").show();
    })
});

我怎样才能让它只搜索表格第一列中的数据?

【问题讨论】:

    标签: javascript jquery html filter


    【解决方案1】:

    试试这个:

    rows.filter(function () {
        return $(this).children(':eq(0)').text().indexOf(v) !== -1;
    }).show();
    

    而不是:

    rows.filter(":contains(" + v + ")").show();
    

    文档:http://api.jquery.com/eq-selector/.

    【讨论】:

    • 感谢您的回答,只是想知道如何准确匹配 v 的值,例如如果 v 为 3,则将包括 13 或 23。无论如何要完全匹配 3?
    • @PedroOliveira 我显然被:contains() 选择器误导了。此解决方案是否符合您的要求(严格相等):$.trim($(this).children(':eq(0)').text()) === v?
    • @PedroOliveira De nada。
    【解决方案2】:

    你不需要遍历行,试试这个,

    rows.filter(function(){
        return (data.indexOf($.trim($(this).children('td').first().text())) != -1);
    }).show();
    

    或者

    rows.filter(function(){
        return $.inArray($.trim($(this).children('td').first().text()),data);
    }).show();
    

    【讨论】:

    • 当然。您显示的是单元格而不是行,此外,如果给定元素不存在,indexOf 返回 -1。
    • @wared 感谢您指出错误,我还有什么遗漏吗?
    【解决方案3】:
    $.each(data, function(i, v) {
      rows.filter(function(){
        return (data.indexOf($.trim($(this).children('td').first().text())) != -1);
     }).show();
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-17
      • 2013-07-13
      • 2012-04-15
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      相关资源
      最近更新 更多