2014-01-17 2 views
0

500 li 's 목록에서 검색하고 있습니다. 다음 코드를 사용합니다. 그러나 두 가지 문제에 직면 해있다. 하나는 내가 뭔가를 입력 한 후 매우 빨리 backspace를 누르면 캡처되지 않습니다. 그리고 또한 검색은 내가 원하지 않는 대소 문자를 구분합니다.jquery를 사용하여 LI에서 검색

$('#find_question').bind("keyup", function() { 
    searchWord = $(this).val(); 
    console.log("input length",searchWord); 
    if (searchWord.length >= 0) { 

     $('#leftSection li').each(function(i, data) { 
      text = $(this).text(); 
      if (text.match(RegExp(searchWord, 'i'))) 
       $(this).show(); 

      else 
       $(this).hide(); 
     }); 
    } 
}); 

답변

1

How do I make jQuery Contains case insensitive, including jQuery 1.8+?

Live Demo

$.expr[':'].containsIgnoreCase = function (n, i, m) { 
    return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; 
}; 

$function() { 
    $('#find_question').on("keyup", function() { 
    searchWord = $(this).val(); 
    $('#leftSection li').hide(); 
    if (searchWord.length >= 0) { 
     $('#leftSection li:containsIgnoreCase("'+searchWord+'")').show(); 
    } 
    }); 
}); 
에서 containsIgnoreCase 오는이

을 시도해보십시오 아래 코드의 개선을 제안 해주십시오

관련 문제