自动完成:如何避免重复搜索?
我使用 JQuery 插件 autocomplete 作为一种数据集选择器。如果用户从自动完成查找中选择一个值,则会在数据库中查询匹配的数据集。如果用户输入新值,则用户可以输入新数据集。当用户输入现有值而不是从自动完成查找中选择它时,就会出现问题。完成此操作后,不会调用自动完成 .result()
方法,并且不会检索任何数据集。为了解决这个问题,我向输入元素添加了 .blur(function(){$(this).search();});
。这解决了原来的问题。
现在我遇到的问题是 .result()
在查找选择和模糊时触发。我希望 .result()
在查找或模糊时触发选择。我怎样才能做到这一点?
这是我的代码:
$('#groupset').autocomplete('ajax/php/leeruns.php');
$('#groupset').result(
function(event, data, formatted) {
if(data){
$('#groupsetdesc').val(formatted);
groups.load(data[1]); //retrieve matching dataset
} else {
$('#groupsetdesc').val('');
}
}
).blur(function(){$(this).search();});
I use JQuery plugin autocomplete as a kind of dataset chooser. If the user chooses a value from the autocomplete lookup, the database is queried for the matching dataset. If the user types in a new value, the user can enter a new dataset. An issue arises when the user types in an existing value rather than choosing it from the autocomplete lookup. When this is done, the autocomplete .result()
method is not called and no dataset is retrieved. To fix this I added a .blur(function(){$(this).search();});
to the input element. This fixed the original problem.
Now I have the problem that .result()
fires on selection from lookup AND on blur. I would like .result()
to fire on selection from lookup OR on blur. How do I make that happen?
Here is my code:
$('#groupset').autocomplete('ajax/php/leeruns.php');
$('#groupset').result(
function(event, data, formatted) {
if(data){
$('#groupsetdesc').val(formatted);
groups.load(data[1]); //retrieve matching dataset
} else {
$('#groupsetdesc').val('');
}
}
).blur(function(){$(this).search();});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将数据的当前值存储在名为 previous 的持久变量中,如果相同则不加载数据集。
You could store the current value of data in a persistent variable called previous and not load the dataset if it is the same.