Skip to content

Commit a7e84a4

Browse files
author
Tomas Kirda
committed
Ignore "change" event when input value is set by plugin, fixes devbridge#669
1 parent 57528dd commit a7e84a4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/jquery.autocomplete.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@
192192
container.children('.' + selected).removeClass(selected);
193193
});
194194

195-
196195
// Listen for click event on suggestions list:
197196
container.on('click.autocomplete', suggestionSelector, function () {
198197
that.select($(this).data('index'));
@@ -457,6 +456,11 @@
457456
},
458457

459458
onValueChange: function () {
459+
if (this.ignoreValueChange) {
460+
this.ignoreValueChange = false;
461+
return;
462+
}
463+
460464
var that = this,
461465
options = that.options,
462466
value = that.el.val(),
@@ -856,6 +860,7 @@
856860
if (that.selectedIndex === 0) {
857861
$(that.suggestionsContainer).children().first().removeClass(that.classes.selected);
858862
that.selectedIndex = -1;
863+
that.ignoreValueChange = false;
859864
that.el.val(that.currentValue);
860865
that.findBestHint();
861866
return;
@@ -898,8 +903,14 @@
898903
}
899904

900905
if (!that.options.preserveInput) {
906+
// During onBlur event, browser will trigger "change" event,
907+
// because value has changed, to avoid side effect ignore,
908+
// that event, so that correct suggestion can be selected
909+
// when clicking on suggestion with a mouse
910+
that.ignoreValueChange = true;
901911
that.el.val(that.getValue(that.suggestions[index].value));
902912
}
913+
903914
that.signalHint(null);
904915
},
905916

0 commit comments

Comments
 (0)