【发布时间】:2020-07-02 00:24:47
【问题描述】:
我有输入字段和提交按钮....当我在输入字段中输入一些文本时,它在我提交之前开始过滤(输入提交按钮)。我该如何纠正。
我想在点击提交按钮后进行过滤。
$(function() {
var $grid = $('#container');
$grid.isotope({itemSelector: '.item'});
var filters = []; // A convenient bucket for all the filter options,
// just so we don't have to look them up in the DOM every time.
// (a global array is maybe sort of not the most elegant
// way you could deal with this but you get the idea.)
// Search event handlers
$('.quicksearch').on('keyup', function() {
// debounce removed for brevity, but you'd put it here
filters[0] = this.value;
runFilter();
});
$('#filter-select').on('change', function() {
filters[1] = this.value;
runFilter();
});
// and so on if more filters needed
// The filter itself
var runFilter = function() {
$grid.isotope({
filter: function() {
if (filters[0]) {
// at least some search text was entered:
var qsRegex = new RegExp(filters[0], 'gi');
// if the title doesn't match, eliminate it:
if (!$(this).find('.content-title').text().match(qsRegex)) {
return false;
}
}
if (filters[1]) {
// a category was selected; filter out others:
if (!($(this).hasClass(filters[1]))) {
return false;
}
}
// etcetera, for any other filters
// successfully passed all conditions, so:
return true;
}
});
}
});
【问题讨论】:
-
每次输入字母时,您的代码都会以
$('.quicksearch').on('keyup', function() {运行。您需要将其更改为单击按钮时。所以请改用$('.your-button-class').on('click',function() {之类的东西。 -
是的!......我明白你在说什么......但我不知道如何做到这一点......所以你能提供一些演示。
-
好的,我在下面为您提供了细分。因为您需要输入键和按钮,所以我向您介绍了如何为 only 输入键(这可能是您之前尝试做的)执行 keyup 事件。很简单,你现在就知道怎么做了 :) 你特意问了怎么做按钮点击,但你也说要讨论回车键。
标签: javascript jquery html