【问题标题】:How to get JQuery-UI autocomplete to submit on pressing enter while using categories from remote source?如何在使用来自远程源的类别时让 JQuery-UI 自动完成以按 Enter 提交?
【发布时间】:2015-02-05 04:33:11
【问题描述】:

我正在使用来自 jqueryui 站点的以下样板代码: http://jqueryui.com/autocomplete/#categories http://jqueryui.com/autocomplete/#remote

$.widget("custom.catcomplete", $.ui.autocomplete, {
    _create: function() {
        this._super();
        this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
    },
    _renderMenu: function(ul, items) {
        var that = this,
            currentCategory = "";
        $.each(items, function(index, item) {
            var li;
            if (item.category != currentCategory) {
                ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
                currentCategory = item.category;
            }
            li = that._renderItemData(ul, item);
            if (item.category) {
                li.attr("aria-label", item.category + " : " + item.label);
            }
        });
    },
});

$(function() {

    $("#search").catcomplete({
        delay: 0,
        source: "search",
        select: function(event, ui) {
            ajax.get("/displayInfo", {
                a: ui.item.value
            }, displayInfo, true);
        }
    });
});

但是即使用户只是按下回车,我如何获得输入触发器“选择”?现在,如果您只是按 Enter 键(不从下拉菜单中选择任何内容),下拉菜单就会消失并且什么也不会发生。但我不想将下拉列表中的任何内容(即使它不匹配)发送到 /displayInfo。

有什么建议吗?

【问题讨论】:

  • 当建议列表为openui 参数为空...我认为这并不容易,您可能需要提出功能请求@bugs.jqueryui.com
  • 我想这是一个非常常见的用例,我认为肯定有其他人已经有这个要求,或者想出了一个解决方法..
  • 嗯,这对我来说也是一个常见的用例,这就是为什么我建议提出功能请求.. :)

标签: javascript jquery jquery-ui autocomplete


【解决方案1】:

当出现自动完成搜索时,使用箭头键并按回车键,它会自动将值填充到文本框中。 要么 您可以使用 onkeyup 或 onkeypress 事件,然后为 Enter 找到 event.keycode,然后使用功能。

【讨论】:

  • 这会扼杀最终用户的体验。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多