Need help to make jQuery .find case insensitive

后端 未结 2 1091
长发绾君心
长发绾君心 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: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();
              }
            }
          }
        });
     }); 
    

提交回复
热议问题