javascript - 清除文本和重置搜索输入的按钮不起作用

标签 javascript jquery

好的,所以我有一个可过滤的搜索表单,可以在网格中返回某些图像,效果很好,当我删除搜索输入中的文本时,它会重置,但是当我单击“清除”按钮时,这应该执行与删除文本相同,它不起作用。以下是使用的 HTML 和 JQuery:

    <form id="live-search" action="" class="styled" method="post" style="margin: 2em 0;">
        <div class="form-group">
            <input type="text" class="form-control" id="filter" value="" style="width: 80%; float: left;" placeholder="Type to search"/>
            <span id="filter-count"></span>
            <input type="button" class="clear-btn" value="Clear" style="background: transparent; border: 2px solid #af2332; color: #af2332; padding: 5px 15px; border-radius: 3px; font-size: 18px; height: 34px;">
        </div>
     </form>

这是用于清除文本的 JQuery:

    jQuery(document).ready(function(){
          jQuery("#filter").keyup(function(){

                            // Retrieve the input field text and reset the count to zero
                            var filter = jQuery(this).val(), count = 0;

                            // Loop through the comment list
                            jQuery(".watcheroo").each(function(){

                                jQuery(this).removeClass('active');

                                // If the list item does not contain the text phrase fade it out
                                if (jQuery(this).text().search(new RegExp(filter, "i")) < 0) {

                                jQuery(this).fadeOut();

                                // Show the list item if the phrase matches and increase the count by 1
                                } else {

                                jQuery(this).show();
                                count++;

                                }

                                });

                            // Update the count
                            var numberItems = count;

                    });
                    //clear button remove text
                    jQuery(".clear-btn").click( function() {
                            jQuery("#filter").value = "";

                    });

    });  

任何帮助将不胜感激。

最佳答案

value 是 DOMElement 上的属性,而不是 jQuery 对象。使用 val('') 代替:

jQuery(document).ready(function($) {
    $("#filter").keyup(function() {
        var filter = $(this).val(), 
            count = 0;

        $(".watcheroo").each(function(){
            var $watcheroo = $(this);
            $watcheroo.removeClass('active');

            if ($watcheroo.text().search(new RegExp(filter, "i")) < 0) {
                $watcheroo.fadeOut();
            } else {
                $watcheroo.show();
                count++;
            }
        });
        var numberItems = count;
    });

    $(".clear-btn").click(function() {
        $("#filter").val(''); // <-- note val() here
    });
}); 

请注意,我修改了您的代码,为传入 document.ready 处理程序的 jQuery 实例添加别名。这样您仍然可以在该函数的范围内安全地使用 $ 变量。

关于javascript - 清除文本和重置搜索输入的按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40552866/

相关文章:

jquery - Shim 命令在 RequireJS 中工作

javascript - 如何取消静音(并关闭自动播放)二十七岁的 YouTube 视频?

javascript - 使用 React 和 Bacon 从节点获取 EventStream

javascript - 如何使用 javascript 将大量选择选项移动到另一个选择控件?

javascript - 使用 img 动态创建 div 并将其附加到现有的 div

javascript - jQuery Accordion 菜单 : scroll to active menu item

javascript - 将输入文本限制为仅数字,电话号码的最小长度为 10

javascript - 如何指定websocket连接握手的URL?

javascript - 与数组元素的数据绑定(bind)

javascript - JQuery 和 Javascript : Intermittant mis-matching of clicked div and returned div?