jquery 自动完成行为修改

发布于 2024-12-10 21:30:21 字数 567 浏览 0 评论 0原文

我终于有了一些自动完成功能,但我想改变一种行为。如果用户输入部分值并单击返回列表之外的任何位置,则部分值将在输入中设置。如果用户没有选择返回的选项之一,我希望该值保持空白。

看起来焦点事件是我应该修改的,但我发现 API 令人困惑......它说:

Before focus is moved to an item (not selecting), ui.item refers to the focused 
item. The default action of focus is to replace the text field's value with the 
value of the focused item, though only if the focus event was triggered by a 
keyboard interaction. Canceling this event prevents the value from being updated,
but does not prevent the menu item from being focused.

但它没有给出如何取消事件的示例。

I've got some autocompletes working finally, but there is one behavior I'd like to change. If the user types in a partial value and clicks anywhere outside of the list returned, the partial value gets set in the input. I would prefer that the value remain blank if the user doesn't select one of the returned options.

It looks like the focus event is what I should be modifying, but I find the API confusing... it says:

Before focus is moved to an item (not selecting), ui.item refers to the focused 
item. The default action of focus is to replace the text field's value with the 
value of the focused item, though only if the focus event was triggered by a 
keyboard interaction. Canceling this event prevents the value from being updated,
but does not prevent the menu item from being focused.

but it doesn't give an example of how to cancel the event.

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

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

发布评论

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

评论(1

夏天碎花小短裙 2024-12-17 21:30:21

在另一个问题中找到了答案:)像这样修改更改事件似乎可以解决问题:

$('#myID').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "cfc/autoSuggest.cfc?method=someMethod&returnformat=json",
                    dataType: "json",
                    data: {
                      search: request.term,
                      maxRows: 20
                    },
                    success: function(data) {
                      response(data);
                    }                   
                })
            },
            change: function(event, ui) {     
                if (!ui.item) {
                    $(this).val('');
                }
            }
        });

Found the answer in another question :) modifying the change event like so seems to do the trick:

$('#myID').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "cfc/autoSuggest.cfc?method=someMethod&returnformat=json",
                    dataType: "json",
                    data: {
                      search: request.term,
                      maxRows: 20
                    },
                    success: function(data) {
                      response(data);
                    }                   
                })
            },
            change: function(event, ui) {     
                if (!ui.item) {
                    $(this).val('');
                }
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文