2012-04-24 48 views
0

嗨即時嘗試做一個過濾器的跨度即時通訊有困難把什麼是跨越小寫或忽略情況。 有什麼建議嗎?在過濾器中的Javascript tolower/ignoreCase

$("#filterinput2").keyup(function() { 
      var filter = ($(this).val()).toLowerCase(); // get the value of the input, which we filter on 
      console.log("lower filter" + filter); 
      if (filter) {   
       // hide tr's for songs not matching and show tr's for songs matching 
       $(".tonelist").find(".song:not(:contains(filter))").parent().parent().parent().fadeOut("slow"); 
       $(".tonelist").find(".song:contains(filter)").parent().parent().parent().fadeIn(); 
       if (window.console != undefined) { 
        console.log($(".tonelist").find(".song:not(:contains(" + filter + "))").parent().parent().parent().innerHTML); 
       } 

       $(".tonelist").find(".grouptitle:not(:contains(" + filter + "))").parent().fadeOut("slow"); 
       $(".tonelist").find(".grouptitle:contains(" + filter + ")").parent().fadeIn(); 
       if (window.console != undefined) 
        console.log($(".tonelist").find(".grouptitle:not(:contains(" + filter + "))").parent().parent().parent().innerHTML); 
      } else 
      // if input field is empty, show all tr's 
       $(".tonelist").find("tr").fadeIn(); 

      ColorLines(); 
     }); 

回答

0

你將不得不自己檢查每個跨度的內容。您可以使用.html()函數將每首歌/ grouptitle的內容作爲字符串來獲取。這樣的事情可能工作(雖然我沒有測試過):

$("#filterinput2").keyup(function() { 
     var filter = ($(this).val()).toLowerCase(); // get the value of the input, which we filter on 
     console.log("lower filter" + filter); 
     if (filter) {   
      // hide tr's for songs not matching and show tr's for songs matching 
      spans = $(".tonelist").find(".song, .grouptitle") 
      for (s in spans) { 
       el = $(spans[s]); 
       if (el.html().match(new RegExp(filter, "i"))) { 
        el.fadeIn(); 
       } else { 
        el.fadeOut("slow"); 
       } 
      } 
     } else 
     // if input field is empty, show all tr's 
      $(".tonelist").find("tr").fadeIn(); 

     ColorLines(); 
    }); 

的原因是:含有()CSS選擇器是大小寫敏感的,而且也沒有辦法,我知道要改變這種狀況。