Skip to content

Commit e3e27bb

Browse files
author
Tomas Kirda
committed
Implement selection invalidation.
1 parent a2bea84 commit e3e27bb

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

scripts/demo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ $(function () {
4747
},
4848
onHint: function (hint) {
4949
$('#autocomplete-ajax-x').val(hint);
50+
},
51+
onInvalidateSelection: function() {
52+
$('#selction-ajax').html('You selected: none');
5053
}
5154
});
5255

src/jquery.autocomplete.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
};
101101
that.hint = null;
102102
that.hintValue = '';
103+
that.selection = null;
103104

104105
// Initialize and set options:
105106
that.initialize();
@@ -178,6 +179,7 @@
178179
that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); });
179180
that.el.on('blur.autocomplete', function () { that.onBlur(); });
180181
that.el.on('focus.autocomplete', function () { that.fixPosition(); });
182+
that.el.on('change.autocomplete', function (e) { that.onKeyUp(e); });
181183
},
182184

183185
onBlur: function () {
@@ -347,11 +349,10 @@
347349
return;
348350
}
349351

350-
that.findBestHint();
351-
352352
clearInterval(that.onChangeInterval);
353353

354354
if (that.currentValue !== that.el.val()) {
355+
that.findBestHint();
355356
if (that.options.deferRequestBy > 0) {
356357
// Defer lookup in case when value changes very quickly:
357358
that.onChangeInterval = setInterval(function () {
@@ -367,6 +368,11 @@
367368
var that = this,
368369
q;
369370

371+
if (that.selection) {
372+
that.selection = null;
373+
(that.options.onInvalidateSelection || $.noop)();
374+
}
375+
370376
clearInterval(that.onChangeInterval);
371377
that.currentValue = that.el.val();
372378

@@ -654,6 +660,7 @@
654660
that.el.val(that.currentValue);
655661
that.signalHint(null);
656662
that.suggestions = [];
663+
that.selection = suggestion;
657664

658665
if ($.isFunction(onSelectCallback)) {
659666
onSelectCallback.call(that.element, suggestion);

0 commit comments

Comments
 (0)