自动完成:如何避免重复搜索?

发布于 2024-08-24 19:36:12 字数 850 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小耗子 2024-08-31 19:36:12

您可以将数据的当前值存储在名为 previous 的持久变量中,如果相同则不加载数据集。

var loaded_previous;
$('#groupset').autocomplete('ajax/php/leeruns.php');
$('#groupset').result(
function(event, data, formatted) {
    if(data && data[1] != loaded_previous){
        loaded_previous = data[1];
        $('#groupsetdesc').val(formatted);
        groups.load(data[1]); //retrieve matching dataset
    } else {
        $('#groupsetdesc').val('');
    }
}
).blur(function(){$(this).search();});

You could store the current value of data in a persistent variable called previous and not load the dataset if it is the same.

var loaded_previous;
$('#groupset').autocomplete('ajax/php/leeruns.php');
$('#groupset').result(
function(event, data, formatted) {
    if(data && data[1] != loaded_previous){
        loaded_previous = data[1];
        $('#groupsetdesc').val(formatted);
        groups.load(data[1]); //retrieve matching dataset
    } else {
        $('#groupsetdesc').val('');
    }
}
).blur(function(){$(this).search();});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文