Skip to content

Commit c3b282f

Browse files
can3pscottgonzalez
authored andcommitted
Autocomplete: Prevent keypress events caused by enter key when selecting an item. Fixes #6055 - Autocomplete: Selecting an item by pressing enter submits the form in Opera.
1 parent 66346d0 commit c3b282f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

ui/jquery.ui.autocomplete.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ $.widget( "ui.autocomplete", {
2828
},
2929
_create: function() {
3030
var self = this,
31-
doc = this.element[ 0 ].ownerDocument;
31+
doc = this.element[ 0 ].ownerDocument,
32+
suppressKeyPress;
33+
3234
this.element
3335
.addClass( "ui-autocomplete-input" )
3436
.attr( "autocomplete", "off" )
@@ -43,6 +45,7 @@ $.widget( "ui.autocomplete", {
4345
return;
4446
}
4547

48+
suppressKeyPress = false;
4649
var keyCode = $.ui.keyCode;
4750
switch( event.keyCode ) {
4851
case keyCode.PAGE_UP:
@@ -65,6 +68,9 @@ $.widget( "ui.autocomplete", {
6568
case keyCode.NUMPAD_ENTER:
6669
// when menu is open and has focus
6770
if ( self.menu.active ) {
71+
// #6055 - Opera still allows the keypress to occur
72+
// which causes forms to submit
73+
suppressKeyPress = true;
6874
event.preventDefault();
6975
}
7076
//passthrough - ENTER and TAB both select the current element
@@ -91,6 +97,12 @@ $.widget( "ui.autocomplete", {
9197
break;
9298
}
9399
})
100+
.bind( "keypress.autocomplete", function( event ) {
101+
if ( suppressKeyPress ) {
102+
suppressKeyPress = false;
103+
event.preventDefault();
104+
}
105+
})
94106
.bind( "focus.autocomplete", function() {
95107
if ( self.options.disabled ) {
96108
return;

0 commit comments

Comments
 (0)