【问题标题】:jquery search list name from table [duplicate]jquery从表中搜索列表名称[重复]
【发布时间】:2014-09-17 04:16:56
【问题描述】:

我想通过this link进行搜索 但我有multiple row 可以在td 值中搜索,那么我如何添加来自所有td 的数据进行搜索? 谢谢!

$("#search").on("keyup", function() {
    var value = $(this).val();

    $("table tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $row.find("td:first").text();

            if (id.indexOf(value) !== 0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    这里是link

    新的jquery:

    $("#search").on("keyup", function() {
        var value = $(this).val();
    
        $("table tr").each(function(index) {
            if (index !== 0) {
    
                $row = $(this);
    
               $row.find("td").each(function(){
                   var id =$(this).text();
    
                if (id.indexOf(value) !== 0) {
                    $row.hide();
                }
                else {
                    $row.show();
                }
               });
            }
        });
    });
    

    【讨论】:

    • 我的代码已经可以用于搜索一列中的数据,但我上面问的是我想使用多列结果中的数据进行搜索
    • 是的,我放在那里的代码是用于在多列中搜索的测试它,你会看到
    • 但是你的代码,我测试不工作:(
    • soo 很抱歉听到这个消息,但你能向我解释一下你到底想做什么吗?我不退出明白你很抱歉:(
    • 通过上面给出的链接,我已经可以用搜索数据测试一列,但我想用更多列的数据进行搜索
    【解决方案2】:

    您可以使用filter() 在当前行中查找与文本匹配的td 元素。试试这个:

    var $row = $(this);
    var tdMatches = $('td', $row).filter(function() {
        return $(this).text().indexOf(value) != -1;
    }).length;
    tdMatches == 0 ? $row.hide() : $row.show()
    

    Updated fiddle

    【讨论】:

      【解决方案3】:

      试试

      $("#search").on("keyup", function () {
          var value = this.value.trim();
          if (value) {
              var data = $("table tr td").filter(function () {
                  return value == $(this).text();
              }).parent();
              $("table tr:gt(0)").hide();
              data.show();
          } else {
              $("table tr").show();  // all data show if the value is 0 If you want do or remove that
          }
      });
      

      DEMO

      【讨论】:

      • @Time 检查此链接jsfiddle.net/rFGWZ/1081
      • 您的演示不起作用,我的任何代码都可以用于第一列中的数据进行搜索,但我上面问的是我想使用多列结果中的数据进行搜索
      猜你喜欢
      • 1970-01-01
      • 2021-08-22
      • 2018-09-25
      • 2012-04-26
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多