【问题标题】:Filtering On Multiple Search Criteria With jQuery使用 jQuery 过滤多个搜索条件
【发布时间】:2017-06-08 07:30:15
【问题描述】:

我在 JavaScript / jQuery 中搜索时遇到问题。例如,如果我选择“双服务”和“在线状态”,则下面的此函数将返回 all“双服务”和所有“在线服务”。

所需的行为有 3 个部分:

1) 不同的搜索条件是 additive(即 AND 而非 OR)。

2) 当搜索条件为空时,返回所有记录。

3) 从#search 输入中进行data-name 搜索不区分大小写

$(document).ready(function() {
  $("#service").change(function(){ select(); });
  $("#search").keyup(function(){ select(); });
  $("#status").change(function(){ select(); });

  select = function(){
    var service = $("#service").val();
    var search = $("#search").val();
    var status = $("#status").val();

    $(".box").hide();

    $(".box[data-service='" + service + "']").show();
    $(".box[data-search='" + search + "']").show();
    $(".box[data-status='" + status + "']").show();

  }

});

示例:https://jsfiddle.net/L7wyp13q/

【问题讨论】:

    标签: javascript jquery search input


    【解决方案1】:

    这里的想法是根据过滤条件的值动态构建选择器。我使用了一个对象来包含过滤器值,以便我可以遍历它们并重用属性名称,但是您可以按照自己的意愿进行操作。

    编辑 -- 更新为从搜索字段返回部分匹配,而不仅仅是完全匹配

    编辑 2 -- 添加不区分大小写的搜索

    $(document).ready(function() {
      $("#service, #status").change(function(){ select(); });
      $("#search").keyup(function(){ select(); });
    
      select = function(){
        var selectorFilters = {
          service: $("#service").val(),
          status: $("#status").val()
        };
    
        $(".box").hide();
    
        var selector = Object.keys(selectorFilters).reduce(function (sel, currentKey) {
          var value = selectorFilters[currentKey];
          if (value && value !== "" && value !== "all") {
            sel += "[data-" + currentKey + "=" + value + "]"; 
          }
          return sel;
        }, ".box"); // <-- start with .box and build from there
    
        // will select even if search is partial match, rather than only exact match
        $(selector).filter(function (){
          var searchValue = $("#search").val().toLowerCase();
          return (searchValue === "" || $(this).data("name").toLowerCase().indexOf(searchValue) > -1);
        }).show();
      }
    });
    

    Working DEMO

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      $(document).ready(function() {
          $("#service").change(function() {
            select();
          });
          $("#search").keyup(function() {
            select();
          });
          $("#status").change(function() {
            select();
          });
      
          select = function() {
            var service = $("#service").val();
            var search = $("#search").val();
            var status = $("#status").val();
      
            $(".box").hide();
            var boxes = $(".box").filter(function(index) {
      
              return (service === 'all' || $(this).attr("data-service") === service) &&
                (!search || $(this).attr("data-name").toLowerCase().indexOf(search.toLowerCase()) >= 0 ) &&
                (status === 'all' || $(this).attr("data-status") === status);
            });
            console.log(boxes);
            boxes.show();
      
          }
      
        });
      

      小提琴演示:https://jsfiddle.net/xcrmyzgr/2/

      【讨论】:

      • 你的例子,我很喜欢。选择“服务”和“状态”效果很好。问题仍然在“搜索”中。例如,在输入中写入“bc”或“BC”的值 - 返回为空。在我看来,结果应该是:AbCdE (4x) 和 BcDeF (4x)。
      • @Tinware 查看我的解决方案,我想这就是您要寻找的一切
      • @Tinware 你也可以再看看我的回答,拜托
      • @Quentin Roger 有问题,我无法理解。看:“a”返回空,“A”返回空,“b”只返回“AbCdE”没有“BcDeF”,“B”返回“AbCdE”没有“BcDeF”
      • @Tinware:请告诉我们您使用的是什么浏览器?我的示例适用于所有浏览器(即使 IE 8 也没有问题),并且搜索栏在开始时确实在搜索。
      猜你喜欢
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2022-01-13
      相关资源
      最近更新 更多