gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:02:02 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com