jQuery 自动完成问题 - 如果用户没有专门选择,则不匹配

发布于 2024-09-11 06:39:29 字数 621 浏览 2 评论 0原文

继我之前的两个问题之后:

如何让 jQuery 自动完成在用户点击提交时执行 search()

我如何修剪 JQuery 自动完成框的输入?

我的表单中有一个 jQuery UI 1.8 自动完成框,大部分情况下它的工作方式如下一种魅力。我遇到的一个问题是,如果我输入有效名称,但不选择出现的选项,则该名称永远不会与我的{name,value}匹配列表,因此未设置包含“值”部分的隐藏输入的正确值。这当然会导致令人困惑的情况,人们可以输入一个您知道是正确的名称,点击提交并被告知您没有输入正确的名称(因为该值为 null)。

即使用户没有从自动完成框中做出选择,如何确保设置了隐藏的输入值?是否有一些功能可以与提交按钮的 onclick 事件联系起来,这将使 jQuery 搜索框中的内容并自动选择第一个选项?或者,我可以使自动完成功能在用户以任何方式取消选择输入框时(返回/选项卡/单击关闭/等)选择第一个选项吗?

谢谢。

Following on from my previous two questions:

How to make jQuery autocomplete execute a search() when user hits submit

How would I trim the input to a JQuery auto-complete box?

I have a jQuery UI 1.8 autocomplete box in my form, and for the most part it works like a charm. The one problem I have is that if I enter a valid name, but do not select the choice that appears, then the name is never matched against my {name,value} list and so the correct value for the hidden input that contains the 'value' part is not set. This of course causes the confusing situation where one can enter a name you know is correct, hit submit and be told you haven't entered a correct name (as the value is null).

How can I make sure that the hidden input value is set even if the user doesn't make a choice from the autocomplete box? Is there some function I can tie to the onclick event of the submit button which would make jQuery do a search with what is in the box and automatically select the first option? Alternatively, can I make it so that the autocomplete will select the first option when the user deselects the input box in any way (return/tab/click off/etc)?

Thanks.

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

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

发布评论

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

评论(1

雪若未夕 2024-09-18 06:39:29

这是我对您问题的解决方案。我刚刚添加了有关更改事件的部分。
我希望它有帮助。

jQuery(function(){
    jQuery('#Resource').autocomplete({
        source: data,
        focus: function(event, ui) {
            jQuery('#Resource').val(ui.item.label);
            return false;
        },          
        select: function(event, ui) {
            jQuery('#Resource').val(ui.item.label);
            jQuery('#EmployeeNumber').val(ui.item.value);
            return false;
        },
        change: function(event,ui){
                for (var i in data){
                    if (data[i].label==jQuery('#Resource').val()){
                        jQuery('#EmployeeNumber').val(data[i].value);
                    }
                }
        }
    });
}); 

Here is my solution to your problem. I just added the part regarding the change event.
I hope it helps.

jQuery(function(){
    jQuery('#Resource').autocomplete({
        source: data,
        focus: function(event, ui) {
            jQuery('#Resource').val(ui.item.label);
            return false;
        },          
        select: function(event, ui) {
            jQuery('#Resource').val(ui.item.label);
            jQuery('#EmployeeNumber').val(ui.item.value);
            return false;
        },
        change: function(event,ui){
                for (var i in data){
                    if (data[i].label==jQuery('#Resource').val()){
                        jQuery('#EmployeeNumber').val(data[i].value);
                    }
                }
        }
    });
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文