2013-12-09 50 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();  
})