2013-09-29 74 views
0

我想通过搜索框搜索列表。这是我的HTML:通过一个列表搜索通过jquery(不区分大小写)

<input type="text" id="query" value=""/> 
<ol id="abbreviations" > 
<li>Cars</li> 
<li>bars</li> 
. 
. 

<ol> 

这是jQuery的代码,我有:

<script> 

var abbrs = {}; 

$('ol#abbreviations li').each(function(i){ 
    abbrs[this.firstChild.nodeValue] = i; 
}); 

$('#query').focus().keyup(function(e){ 
    if(this.value.length >= 2){ 
     $('ol#abbreviations li').hide(); 
     var filterBy = this.value; 
     for (var abbr in abbrs) { 
      if (abbr.indexOf(filterBy) !== -1) { 
       var li = abbrs[abbr]; 
       $('ol#abbreviations li:eq('+li+')').show(); 
      } 
     }  
    } else { 
     $('ol#abbreviations li').show();  
    } 
    if(e.keyCode == 13) { 
     $(this).val(''); 
     $('ol#abbreviations li').show(); 
    } 
}); 
</script> 

我的代码是可以正常使用,但它是区分大小写的。例如,如果我输入“汽车”它将过滤列表,但如果我输入“汽车”它不起作用。那么我该如何让这个jQuery搜索具有整体性?我试图改变var filterBy = this.value.toLowerCase();,但它不会匹配大写字母。我怎样才能使它工作?

+3

你可以比较这两个列表,并以小写的值。 –

+0

感谢提示:) @JakubMichálek+1,我让它工作:) – Vishnu

回答

1

你需要转换为小写两个filterValueabbr

var filterBy = this.value.toLowerCase(); 
... 
if (abbr.toLowerCase().indexOf(filterBy) !== -1) { 
... 

另见this SO question

+0

+1工作谢谢:)将很快接受答案 – Vishnu