Skip to content

Commit ab1c27b

Browse files
author
Tomas Kirda
committed
Do not trigger select on valid input when multiple suggestions are available, fixes devbridge#361
1 parent afdece2 commit ab1c27b

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

scripts/countries.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var countries = {
22
"AD": "Andorra",
3+
"A2": "Andorra Test",
34
"AE": "United Arab Emirates",
45
"AF": "Afghanistan",
56
"AG": "Antigua and Barbuda",

src/jquery.autocomplete.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@
464464
var that = this,
465465
options = that.options,
466466
value = that.el.val(),
467-
query = that.getQuery(value),
468-
index;
467+
query = that.getQuery(value);
469468

470469
if (that.selection && that.currentValue !== query) {
471470
that.selection = null;
@@ -477,12 +476,9 @@
477476
that.selectedIndex = -1;
478477

479478
// Check existing suggestion for the match before proceeding:
480-
if (options.triggerSelectOnValidInput) {
481-
index = that.findSuggestionIndex(query);
482-
if (index !== -1) {
483-
that.select(index);
484-
return;
485-
}
479+
if (options.triggerSelectOnValidInput && that.isExactMatch(query)) {
480+
that.select(0);
481+
return;
486482
}
487483

488484
if (query.length < options.minChars) {
@@ -492,19 +488,10 @@
492488
}
493489
},
494490

495-
findSuggestionIndex: function (query) {
496-
var that = this,
497-
index = -1,
498-
queryLowerCase = query.toLowerCase();
491+
isExactMatch: function (query) {
492+
var suggestions = this.suggestions;
499493

500-
$.each(that.suggestions, function (i, suggestion) {
501-
if (suggestion.value.toLowerCase() === queryLowerCase) {
502-
index = i;
503-
return false;
504-
}
505-
});
506-
507-
return index;
494+
return (suggestions.length === 1 && suggestions[0].value.toLowerCase() === query.toLowerCase());
508495
},
509496

510497
getQuery: function (value) {
@@ -668,15 +655,11 @@
668655
category = currentCategory;
669656

670657
return '<div class="autocomplete-group"><strong>' + category + '</strong></div>';
671-
},
672-
index;
658+
};
673659

674-
if (options.triggerSelectOnValidInput) {
675-
index = that.findSuggestionIndex(value);
676-
if (index !== -1) {
677-
that.select(index);
678-
return;
679-
}
660+
if (options.triggerSelectOnValidInput && that.isExactMatch(value)) {
661+
that.select(0);
662+
return;
680663
}
681664

682665
// Build suggestions inner HTML:

0 commit comments

Comments
 (0)