点击清除输入搜索



我有一个小脚本,通过它们的"alt";使用搜索字段标记。

现在,我尝试通过单击按钮来清除搜索(不仅清空输入字段(。

但我只能通过按下";删除";在我的键盘上,当在球场内。我做错了什么?

$("#search").on("keyup", function() {
if ($(this).val().length > 0) {
$(".image").hide();
var mySelector = $(this).val();
var myImgs = $("[alt*='" + mySelector + "' i]");
myImgs.show();
} else {
$(".image").show();
}
});

否。这没那么容易。我得再解释一下。在网页上有一个功能,用户可以通过两种方式搜索品牌:按类别(带筛选脚本的下拉列表(和使用搜索字段。以下是完整的脚本:

//Brands Filter 
var $btns = $('.btn-brand').click(function () {
if (this.id == 'all') {
$('.brand').removeClass('brand-off');
} else {
var $el = $('.' + this.id).removeClass('brand-off');
$('.brand').not($el).addClass('brand-off');
}
$btns.removeClass('btn-brand-active');
$(this).addClass('btn-brand-active');
});

//Free Search
$("#search").on("keyup", function() {
if ($(this).val().length > 0) {
// hide everything,
$(".brand").hide();
// get the text in the input
var mySelector = $(this).val();
// show any alt that contains the input
var myImgs = $("[alt*='"+mySelector+"' i]");
myImgs.show();
} else {
$(".brand").show();
}
});

因此,如果我使用搜索字段,然后使用下拉列表,我不会得到所有结果,直到我通过单击"手动清除搜索字段;删除";按钮。

最新更新