使用 jquery 自动完成功能匹配用户键入的输入

发布于 2024-11-04 04:39:54 字数 968 浏览 8 评论 0原文

我正在使用 jquery-ui 自动完成来验证某些表单输入。 我有这段代码:

function addAutoComplete(beacon, url) {
    $(document).ready(function(){
        $('input' + beacon).autocomplete({
            source: url,
            minLength:3,
            select: function(event, ui) {
                var selectedObj = ui.item;
                $(beacon+'_autocomplete_id').val(selectedObj.id);
                return false;
            },
            change: function(event, ui){
                if (ui.item == null){
                           $(beacon+'_autocomplete_id').val(0);
                } else {
                   $(beacon+'_autocomplete_id').val(ui.item.id);
                   }

            }
            });
        });
}       

目标很简单:将数据的“id”值与隐藏字段相关联。当用户的输入不在源中时,我们输入“0”。我的问题是,当用户输入所有内容并且不单击或使用自动完成功能时,他可以编写正确的值,但不会被如此确认。

例如,如果将源存储在本地数组中,我已经了解了如何避免此问题,但是有没有办法使用来自 url 的源来做到这一点?换句话说,是否有一个属性允许我操作从 url 获取的 JSON,而无需为源代码编写回调函数?

提前致谢 !

I'm using jquery-ui autocomplete to validate some form inputs.
I've got this code :

function addAutoComplete(beacon, url) {
    $(document).ready(function(){
        $('input' + beacon).autocomplete({
            source: url,
            minLength:3,
            select: function(event, ui) {
                var selectedObj = ui.item;
                $(beacon+'_autocomplete_id').val(selectedObj.id);
                return false;
            },
            change: function(event, ui){
                if (ui.item == null){
                           $(beacon+'_autocomplete_id').val(0);
                } else {
                   $(beacon+'_autocomplete_id').val(ui.item.id);
                   }

            }
            });
        });
}       

The goal is simple : associate the "id" value of the datas with an hidden field. When the user's input is not in the source, we put a "0". My problem is that when the user types everything and do not click or make use of the autocomplete, he could write a proper value but it would not be acknowleged as such.

I've seen how to avoid this problem if you store the source in a local array, for instance, but is there a way to do this with the source coming from the url ? In other terms, is there a property that allow me to manipulate the JSON I go from the url without having to code my callback function for the source ?

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你怎么这么可爱啊 2024-11-11 04:39:54

找到了一个解决方案:使用 Scott González 很棒 自动选择选项。工作完美,真的很棒。

Found a solution : using Scott González wonderful autoSelect option. Works perfectly, really great.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文