延迟输入上的JavaScript,因此输入到搜索字段的每个字符确实会发射功能
我有一个搜索字段,可让用户过滤从数据库返回的结果。我设置了设置,以便搜索字段具有.on('input',function(){
,它将触发另一个函数。
这会提出一个问题,在这种问题中,如果用户搜索“ crumble “将为用户输入的每个字符触发AJAX请求。
是否可以延迟JavaScript,以便如果用户正在搜索产品,则不会针对每个字符启动该功能,但是在检测到时会触发。那是用户尚未进一步键入。即用户在搜索的内容中键入
$(document).ready(function () {
$('#cards-search').on('input', function() {
cards_page = ''
cards_search = this.value;
get_card_data()
});
});
I have a search field to allow users to filter the results that are returned from the database. I have it set so that the search field has a .on('input', function() {
which will trigger another function.
This poses a problem in which if a user was to search for "crumble" the ajax request will be triggered for each character the user types in.
Is there a way to delay the JavaScript so that if the user is searching for a product, the function isn't fired for each character inputted, but will trigger when detected that the user hasn't typed something further in. I.E. when the user is done typing in what that are searching.
Code:
$(document).ready(function () {
$('#cards-search').on('input', function() {
cards_page = ''
cards_search = this.value;
get_card_data()
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下,这称为辩论,以防万一您需要搜索更多有关它的信息
try this, this is called debouncing in case u need to search more about it
// 运行JavaScript时用户完成时用户完成输入而不是键上吗?
// Run javascript function when user finishes typing instead of on key up?
遵循Vlaz建议,我研究了
bebouncing
,这正是我所需要的。我遵循步骤在这里
html
:
Following VLAZ advice I looked into
debouncing
and it is exactly what I needed.I followed the steps here
HTML:
Javascript: