Javascript jQuery实时搜索
我正在使用jQuery的实时搜索方法,在任何人看来,这段代码是否有效,我认为其中有一个bug。虽然代码中有一个bug,但它确实可以工作。我正在使用jQuery的实时搜索方法,在任何人看来,这段代码是否有效,我认为其中有一个bug。虽然代码中有一个bug,但它确实可以工作Javascript jQuery实时搜索,javascript,jquery,regex,Javascript,Jquery,Regex,我正在使用jQuery的实时搜索方法,在任何人看来,这段代码是否有效,我认为其中有一个bug。虽然代码中有一个bug,但它确实可以工作。我正在使用jQuery的实时搜索方法,在任何人看来,这段代码是否有效,我认为其中有一个bug。虽然代码中有一个bug,但它确实可以工作 <script> $(document).ready(function(){ $("#discount_credits").keyup(function(){
<script>
$(document).ready(function(){
$("#discount_credits").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 meals = "+count);
});
});
</script>
<script>
$(document).ready(function(){
$("[type=range]").change(function(){
var newval=$(this).val();
$("#slidernumber").text(newval);
});
});
</script>
<div id="search_wrap">
<form id="live-search" action="" class="styled" method="post">
<fieldset>
<input type="range" min="0" step="1" max="100" name="discount_credits" id="discount_credits">
<span>£</span><span id="slidernumber">25</span>
<span id="filter-count"></span>
</fieldset>
</form>
</div>
<ul class="commentlist">
<li>22.02</li>
<li>21.99</li>
<li>21.99</li>
<li>12.00</li>
<li>42.00</li>
<li>61.99</li>
<li>2.00</li>
</ul>
$(文档).ready(函数(){
$(“#折扣额”).keyup(函数(){
//检索输入字段文本并将计数重置为零
var filter=$(this).val(),count=0;
//循环浏览注释列表
$(“.commentlist li”)。每个(函数(){
//如果列表项不包含文本短语,请将其淡出
if($(this.text().search)(新的RegExp(filter,“i”))<0){
$(this.fadeOut();
//如果短语匹配,则显示列表项并将计数增加1
}否则{
$(this.show();
计数++;
}
});
//更新计数
var numberItems=计数;
$(“#过滤器计数”).text(“用餐次数=”+count);
});
});
$(文档).ready(函数(){
$(“[type=range]”。更改(函数(){
var newval=$(this.val();
$(“#滑块编号”).text(newval);
});
});
£25
- 22.02
- 21.99
- 21.99
- 12:00
- 42.00
- 61.99
- 二点
问题出在您的情况下。它当前在
标记中的值和您在范围中输入的值之间进行正则表达式匹配。要更正此问题,应将内容文本解析为int值,并将其与筛选器进行比较:
// If the list item does not contain the text phrase fade it out
if (parseInt($(this).text()) > filter) {
$(this).fadeOut();
// Show the list item if the phrase matches and increase the count by 1
} else {
$(this).show();
count++;
}