我正在尝试构建一个模拟实时搜索的页面 - 搜索结果显示为用户类型。下面的插件运行良好,除了我想在开头隐藏结果(有序列表)并在用户键入时显示每个匹配的项目符号。
http://www.designchemical.com/blog/index.php/jquery/live-text-search-function-using-jquery/
jQuery
$(document).ready(function(){
$("#filter").keyup(function(){
// Retrieve the input field text and reset the count to zero
var filter = $(this).val(), count = 0;
// Loop through the comment list
$(".commentlist li").each(function(){
// If the list item does not contain the text phrase fade it out
if ($(this).text().search(new RegExp(filter, "i")) < 0) {
$(this).fadeOut();
// Show the list item if the phrase matches and increase the count by 1
} else {
$(this).show();
count++;
}
});
// Update the count
var numberItems = count;
$("#filter-count").text("Number of Comments = "+count);
});
});
HTML
<form id="live-search" action="" class="styled" method="post">
<fieldset>
<input type="text" class="text-input" id="filter" value="" />
</fieldset>
</form>
<ol class="commentlist">
<li>Comment #1</li>
<li>Comment #2</li>
</ol>
任何帮助表示赞赏。