如何在使用远程源类别时按Enter键提交JQuery-UI自动完成功能?

时间:2014-12-07 12:06:37

标签: javascript jquery jquery-ui autocomplete

我正在使用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。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

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