对Enter键执行JQuery AutoComplete操作

Saj*_*Ali 2 jquery

我希望AutoComplete的行为如下.

当用户在文本框中键入内容时,不应该发生任何事情.只有当用户在文本框中完成写入并按下回车键时,才会出现自动填充建议列表.

知道如何做到这一点..或者在哪里更改代码..

Saj*_*Ali 5

步骤1

通过更改具有签名的方法,更改jquery.ui.autocomplete.js文件以接受enter键,如下所示

.bind("keydown.autocomplete",功能(事件)

并改变它的以下代码

                case keyCode.ENTER:
                case keyCode.NUMPAD_ENTER:
                    // when menu is open and has focus
                    if (self.menu.active) {
                        // #6055 - Opera still allows the keypress to occur
                        // which causes forms to submit
                        suppressKeyPress = true;
                        event.preventDefault();
                    }
                    //passthrough - ENTER and TAB both select the current element
Run Code Online (Sandbox Code Playgroud)

                case keyCode.ENTER:
                case keyCode.NUMPAD_ENTER:
                    // when menu is open and has focus
                    if (self.menu.active) {
                        // #6055 - Opera still allows the keypress to occur
                        // which causes forms to submit
                        suppressKeyPress = true;
                        event.preventDefault();
                    }
                    else {

                        clearTimeout(self.searching);
                        self.searching = setTimeout(function () {
                            // only search if the value has changed

                            self.selectedItem = null;
                            self.search(null, event);

                        }, self.options.delay);

                    }
                    //passthrough - ENTER and TAB both select the current element
Run Code Online (Sandbox Code Playgroud)

步骤:2 将自动完成绑定更改为

 $('.SearchAddresses').autocomplete({
  // Your bind code by setting required parameters

  search: function (event, ui) {
                var key = CheckBrowser(event);
                if (key == 13)
                    return true;
                else
                    return false;
            }
   });


 function CheckBrowser(e) {
        if (window.event)
            key = window.event.keyCode;     //IE
        else
            key = e.which;     //firefox
        return key;
    }
Run Code Online (Sandbox Code Playgroud)

SETP:3 如果您在asp.net表单控件中使用它

然后包括它.

 $(document).ready(function () {

        $("form").keypress(function (e) {
            var key = CheckBrowser(e);
            if (key == 13) {
                e.preventDefault();
                return false;
            }
            else {
                return true;
            }
        });
    });
Run Code Online (Sandbox Code Playgroud)