Need help to make jQuery .find case insensitive

后端 未结 2 1092
长发绾君心
长发绾君心 2021-01-26 11:46

Trying to make this jQuery filter that uses .find case-insensitive.

For example, when the search = \"cat\" (no quotes) it will find \"cat\" but not \"Cat\" or \"CAT\". S

相关标签:
2条回答
  • 2021-01-26 12:11

    Convert both strings to lowercase, and then do the search:

    search = $(this).val().toLowerCase();
    
    $li.text().toLowerCase().search(search);
    
    0 讨论(0)
  • 2021-01-26 12:23

    you want to make case insensitive searches. try using regex search(/string/i)

    $("#filter").live("keyup", function (ev) {
        var $filter = $(this),
            keyCode = ev.keyCode || ev.which,
            search;
    
        if (keyCode === 27) { //ESC
          $filter.val("");
        }
    
        search = $(this).val() ;
        // added
        var search_regex = new RegExp(search, 'i');
        $("#results").find("li").each(function () {
          var $li = $(this);
          if (search === "") {
            $li.show();
          } else {
            if (_.include(service_names, search)) {
              $li.toggle($li.hasClass("items-" + search));
            } else {
              // changed here
              if (search_regex.test($li.text())) {
                $li.show();
              } else {
                $li.hide();
              }
            }
          }
        });
     }); 
    
    0 讨论(0)
提交回复
热议问题