当用户按“Enter”键时自动完成停止

发布于 2024-08-05 01:25:37 字数 358 浏览 0 评论 0原文

我在商业网站上使用 AutoCompleteExtender。我的问题是用户快速输入单词的一部分并立即按“Enter”键,这会导致自动完成控件返回建议列表。例如,如果我的数据库中有短语“德克萨斯州,美国”,但用户只需快速键入“德克萨斯州”,然后按 Enter 键,则不会出现下拉列表。

我希望自动完成控件忽略用户已按 Enter 的事实并继续获取建议的数据。 (最终的结果是,当当前没有列表时忽略 Enter,但在有列表时选择一个项目)。

我可以通过转到此 Microsoft ASP.NET 站点的示例部分并快速键入一些字符,然后按“Enter”来模拟这个确切的问题。

请问有人可以告诉我我需要做什么吗?

谢谢,马丁

I am using the AutoCompleteExtender on a commercial site. My problem is the users are quickly typing in part of a word and immediately pressing "Enter" which causes the AutoComplete control to NOT come back with a list of suggestions. For example, if my database has the phrase "Texas, United States" in it but the users just type "Texas" quickly followed by Enter then the dropdown list does not appear.

What I would like is for the AutoComplete control to ignore the fact the user has pressed Enter and go and fetch the suggested data anyway. (The ultimate would be if it ignored Enter when there was currently no list, but selected an item when there was a list).

I can simulate this exact problem by going to the samples section of this Microsoft ASP.NET site and typing in some characters very quickly followed by 'Enter'.

Please could someone tell me what I need to do?

Thanks, Martin

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

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

发布评论

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

评论(2

め可乐爱微笑 2024-08-12 01:25:37

我之前已经解决过这个问题,在作为自动完成目标的文本框上有一个额外的 keydown 处理程序。将下面代码片段中的 this._autoCompleteBehavior 替换为对 AutoCompleteBehavior 实例的引用(可通过 $find()BehaviorID 获取)。这里的想法是强制自动完成行为认为它需要通过调用 _onTimerTick() 来执行查找,该查找在键入延迟到期后执行。默认情况下,通过按 Enter 键可以取消键入延迟,因此这只会强制在 Enter 或 Tab 上进行查找。

免责声明:我的 hack 引用了 AjaxControlToolkit 代码的“私有”成员(以下划线开头的内容是“私有”),因此可能不能保证它是面向未来的。

_searchTextbox_keydown: function(e)
{
    var key = e.keyCode || e.rawEvent.keyCode;

    // If the user hits enter or tab before the auto complete popup appears, force the autocomplete lookup at that moment.        
    if ((key === Sys.UI.Key.enter || key === Sys.UI.Key.tab) && this._autoCompleteBehavior._currentPrefix != this._autoCompleteBehavior._currentCompletionWord())
    {
        this._autoCompleteBehavior._onTimerTick(this._autoCompleteBehavior._timer, Sys.EventArgs.Empty);

        e.preventDefault();
    }
}

I've hacked around this problem before with an extra keydown handler on the textbox that is the target of the auto-complete. Replace this._autoCompleteBehavior in the snippet below with a reference to your instance of AutoCompleteBehavior (obtainable via $find() and the BehaviorID). The idea here is to force the auto-complete behavior to think it needs to perform a lookup by calling _onTimerTick(), which executes after the typing delay has expired. By default the typing delay gets cancelled by hitting the enter key, so this just forces the lookup anyway on enter or tab.

Disclaimer: my hack references "private" members of the AjaxControlToolkit code (stuff that starts with underscore is "private"), so it is probably not guaranteed to be future-proof.

_searchTextbox_keydown: function(e)
{
    var key = e.keyCode || e.rawEvent.keyCode;

    // If the user hits enter or tab before the auto complete popup appears, force the autocomplete lookup at that moment.        
    if ((key === Sys.UI.Key.enter || key === Sys.UI.Key.tab) && this._autoCompleteBehavior._currentPrefix != this._autoCompleteBehavior._currentCompletionWord())
    {
        this._autoCompleteBehavior._onTimerTick(this._autoCompleteBehavior._timer, Sys.EventArgs.Empty);

        e.preventDefault();
    }
}
胡渣熟男 2024-08-12 01:25:37

嘿,尝试一下 jQuery 或 YUI 自动完成扩展器。它将快如闪电。

Hey try jQuery or YUI autocomplete extender. It will be lightning fast.

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