diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index cdfc3bb..2ffa5d0 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -62,6 +62,7 @@ that.suggestions = []; that.badQueries = []; that.selectedIndex = -1; + that.selectedBy = 'other'; // what changed selectedIndex ? possible values: 'keyboard','mouse' or 'other' that.currentValue = that.element.value; that.timeoutId = null; that.cachedResponse = {}; @@ -183,6 +184,7 @@ // Listen for mouse over event on suggestions list: container.on('mouseover.autocomplete', suggestionSelector, function () { + that.selectedBy = 'mouse'; that.activate($(this).data('index')); }); @@ -392,11 +394,13 @@ } return; case keys.TAB: + // select suggestion only if it was selected by keyboard, not by mouse. + // Hint is an exception from this rule: if (that.hint && that.options.onHint) { that.selectHint(); return; } - if (that.selectedIndex === -1) { + if (that.selectedIndex === -1 || that.selectedBy !== 'keyboard') { that.hide(); return; } @@ -406,7 +410,8 @@ } break; case keys.RETURN: - if (that.selectedIndex === -1) { + // select suggestion only if it was selected by keyboard, not by mouse. + if (that.selectedIndex === -1 || that.selectedBy !== 'keyboard') { that.hide(); return; } @@ -857,6 +862,8 @@ return; } + that.selectedBy = 'keyboard'; + if (that.selectedIndex === 0) { $(that.suggestionsContainer).children().first().removeClass(that.classes.selected); that.selectedIndex = -1; @@ -872,6 +879,8 @@ moveDown: function () { var that = this; + that.selectedBy = 'keyboard'; + if (that.selectedIndex === (that.suggestions.length - 1)) { return; }