0

当我按下 ENTER 以防止 jQueryUI 自动完成选择当前聚焦的项目并关闭菜单时,我特别想阻止交互。

我指的是以下文档:http ://api.jqueryui.com/autocomplete/#event-change在此处输入图像描述

4

1 回答 1

1

更新:

原理是一样的。只需更改setTimeout函数以提交您的表单(如此处所示:http: //jsfiddle.net/X8Ghc/8/

更新:

你是对的,$(this)应该指的是ui-menu(显然没有)。新的小提琴http://jsfiddle.net/X8Ghc/7/运行良好。

$(document).ready(function() {
    $("#autocomplete").autocomplete({
        "open": function(e, ui) {
            //using the 'open' event to capture the originally typed text
            var self = $(this),
                val = self.val();
            //saving original search term in 'data'.
            self.data('searchTerm', val);
        },
        "select": function(e, ui) {
            var self = $(this),
                keyPressed = e.keyCode,
                keyWasEnter = e.keyCode === 13,
                useSelection = true,
                val = self.data('searchTerm');
            if (keyPressed) {
                if (keyWasEnter) {
                    useSelection = false;
                    e.preventDefault();
                    window.setTimeout(function() {
                        //since there is apparently no way to prevent this
                        //contemptible menu from closing, re-open the menu
                        //using the original search term after this handler
                        //finishes executing (using 'setTimeout' with a delay
                        //of 0 milliseconds).
                        self.val(val);
                        self.autocomplete('search', val);
                    }, 0);
                }
            }
            return useSelection;
        },
        "source": ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
    });
});​

原来的:

这在小提琴中起作用:

$(document).ready(function() {
    $("#autocomplete").autocomplete({
        "select": function(e, ui) {
            var keyPressed = e.keyCode,
                keyWasEnter = e.keyCode === 13,
                useSelection = true;
            console.log(e);
            if (keyPressed) {
                if (keyWasEnter) {
                    useSelection = false;
                    $(this).open(e, ui);
                }
            }
            return useSelection;
        },
        "source": ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
    });
});​
于 2012-10-27T03:11:25.580 回答