繁体   English   中英

jQuery实时搜索

[英]jQuery live Search

我正在使用jQuery实时搜索的方法,此代码是否在任何人的看来都有效,我认为其中存在错误。 尽管代码中有错误,但它确实有效。 我正在使用jQuery实时搜索的方法,此代码是否在任何人的看来都有效,我认为其中存在错误。 尽管代码中有错误,但它确实有效。

<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>

问题出在您的状况上。 当前,它在<li>标记中的值和您要放置在范围中的值之间进行正则表达式匹配。 要更正此问题,您应该将内容文本解析为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++;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM