2013-12-09 46 views
1

我正在使用以下脚本来筛选表中的结果。只有问题是它是区分大小写的。我将如何去使它不区分大小写?Javascript区分大小写筛选器

<script> 
$(document).ready(function() { 

     $("#searchInput").keyup(function(){ 
    //hide all the rows 
      $("#fbody").find("tr").hide(); 

    //split the current value of searchInput 
      var data = this.value.split(" "); 
    //create a jquery object of the rows 
      var jo = $("#fbody").find("tr"); 

    //Recusively filter the jquery object to get results. 
      $.each(data, function(i, v){ 
       jo = jo.filter("*:contains('"+v+"')"); 
      }); 
     //show the rows that match. 
      jo.show(); 
    //Removes the placeholder text 

     }).focus(function(){ 
      this.value=""; 
      $(this).css({"color":"black"}); 
      $(this).unbind('focus'); 
     }).css({"color":"#C0C0C0"}); 

    }); 

</script> 
+1

可能的重复[我如何使jQuery包含大小写不区分大小写,包括jQuery 1.8+?](http://stackoverflow.com/questions/2196641/how-do-i-make-jquery-contains-case-insensitive -including-jquery-1-8) – j08691

+0

让它不区分大小写的一个好方法是将'toLowerCase()'应用于两者,所以'var foo ='HelLO WoRLd!'''var boo ='你好! ''so'foo.toLowerCase()=== boo.toLowerCase()' – Nico

回答

0

我可能会改变滤波器:

 $.each(data, function(i, v) { 
      jo = jo.filter(function() { 
      return $(this).text().toLowerCase().indexOf(v.toLowerCase()) > -1; 
      }); 
     }); 
+0

Legend!谢谢! – user3078580

0
$(document).ready(function() { 

    $("#searchInput").keyup(function(){ 
//hide all the rows 
     $("#fbody").find("tr").hide(); 

//split the current value of searchInput 
     var data = this.value.toLowerCase().split(" "); 
//create a jquery object of the rows 
     var jo = $("#fbody").find("tr"); 

//Recusively filter the jquery object to get results. 
     $.each(data, function(i, v){ 
      jo = jo.filter("*:contains('"+v.toLowerCase()+"')"); 
     }); 
    //show the rows that match. 
     jo.show(); 
//Removes the placeholder text 

    }).focus(function(){ 
     this.value=""; 
     $(this).css({"color":"black"}); 
     $(this).unbind('focus'); 
    }).css({"color":"#C0C0C0"}); 

}); 
1

可以做财产以后这样与filter()

$.each(data, function (i, v) { 
    v = v.toLowerCase();  
    jo.filter(function() { 
     var txt = $(this).text().toLowerCase(); 
     return txt.indexOf(v) > -1; 
    }).show();  
})