简体   繁体   English

不区分大小写的列表过滤

[英]Case Insensitive List Filtering

I'm trying to filter a list without worrying about whether a word is capitalized or not. 我正在尝试过滤列表,而不用担心单词是否大写。

I have the following HTML: 我有以下HTML:

<ul class="dropdown-menu gatherDropDown" id="list">
   <h3 id="header">Filter by </h3>
   <input id="filter" type="text" name="fname" onkeyup="filterList(this)">
   <li><input type="checkbox" class="item_name"> Producer 1</li>
   <li><input type="checkbox" class="item_name"> Producer 2</li>
   <li><input type="checkbox" class="item_name"> Producer 3</li>
   <li><input type="checkbox" class="item_name"> Producer 4</li>
   <li><input type="checkbox" class="item_name"> Producer 5</li>
   <li><input type="checkbox" class="item_name"> Producer 6</li>
   <li><input type="checkbox" class="item_name"> Producer 7</li>
</ul>

Here is my jQuery: 这是我的jQuery:

function filterList(element){
  var value = $(element).val();

  $("#list > li:not(:contains(" + value + "))").hide();
  $("#list > li:contains(" + value + ")").show();
}

I have also set up a codepen: http://codepen.io/tetonhiker/pen/Nbqrzd 我还设置了一个Codepen: http ://codepen.io/tetonhiker/pen/Nbqrzd

Well I've changed your js quite a bit but how about something like this (see the bottom for an explanation): 好吧,我对您的js进行了很多更改,但是这样的事情怎么样(请参阅底部的说明):

 $("#filter").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#list > li").each(function() { $(this).toggle( $(this).text().toLowerCase().indexOf(value) !== -1 ) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h3 id="header">Filter by </h3> <input id="filter" type="text" name="fname"> <ul class="dropdown-menu gatherDropDown" id="list"> <li><input type="checkbox" class="item_name">Producer 1</li> <li><input type="checkbox" class="item_name">Producer 2</li> <li><input type="checkbox" class="item_name">Producer 3</li> <li><input type="checkbox" class="item_name">Producer 4</li> <li><input type="checkbox" class="item_name">Producer 5</li> <li><input type="checkbox" class="item_name">Producer 6</li> <li><input type="checkbox" class="item_name">Producer 7</li> </ul> 

Explanation: I'm looping through each of your list items and checking whether their .text() value has a match for the value of the input box. 说明:我正在遍历每个列表项,并检查它们的.text()值是否与input框的值匹配。 The reason it's not case sensitive is that it makes everything lower case. 它不区分大小写的原因是它使所有内容都变为小写。

you can use this also 你也可以用这个

$.expr[":"].Contains = $.expr.createPseudo(function (arg) {
            return function (elem) {
                return $(elem).text().toLowerCase().indexOf(arg.toLowerCase()) >= 0;
            };
        });
        function filterList(element) {
            var value = $(element).val();
            $("#list > li:not(:Contains(" + value + "))").hide();
            $("#list > li:Contains(" + value + ")").show();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM