2013-12-09 3 views
1

Я использую сценарий ниже, чтобы фильтровать результаты в таблице. Только проблема в том, что он чувствителен к регистру. Как я могу сделать его чувствительным к регистру?Javascript Case Sensitive filter

<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 = 'HELLO world! ''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

Легенда !! Спасибо! – 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

Можно сделать Somthing как это с filter():

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