🌐 AI搜索 & 代理 主页
Skip to content

Commit 034c5f6

Browse files
Add autoselect option
1 parent 842199e commit 034c5f6

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

doc/jquery_typeahead.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ When initializing a typeahead, there are a number of options you can configure.
117117

118118
* `hint` – If `false`, the typeahead will not show a hint. Defaults to `true`.
119119

120+
* `autoselect` – If `true`, the first suggestion will be selected when pressing the Enter key.
121+
120122
* `minLength` – The minimum character length needed before suggestions start
121123
getting rendered. Defaults to `1`.
122124

src/typeahead/plugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
input: input,
8585
menu: menu,
8686
eventBus: eventBus,
87-
minLength: o.minLength
87+
minLength: o.minLength,
88+
autoselect: o.autoselect
8889
}, www);
8990

9091
$input.data(keys.www, www);

src/typeahead/typeahead.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ var Typeahead = (function() {
3939

4040
this.enabled = true;
4141

42+
this.autoselect = !!o.autoselect;
43+
4244
// activate the typeahead on init if the input has focus
4345
this.active = false;
4446
this.input.hasFocus() && this.activate();
@@ -133,6 +135,12 @@ var Typeahead = (function() {
133135

134136
_onDatasetRendered: function onDatasetRendered(type, suggestions, async, dataset) {
135137
this._updateHint();
138+
139+
if(this.autoselect) {
140+
var cursorClass = this.selectors.cursor.substr(1);
141+
this.menu.$node.find(this.selectors.suggestion).first().addClass(cursorClass);
142+
}
143+
136144
this.eventBus.trigger('render', suggestions, async, dataset);
137145
},
138146

@@ -162,9 +170,14 @@ var Typeahead = (function() {
162170
var $selectable;
163171

164172
if ($selectable = this.menu.getActiveSelectable()) {
165-
if (this.select($selectable)) {
166-
$e.preventDefault();
167-
$e.stopPropagation();
173+
if (this.select($selectable)){
174+
$e.preventDefault();
175+
$e.stopPropagation();
176+
}
177+
} else if(this.autoselect) {
178+
if (this.select(this.menu.getTopSelectable())) {
179+
$e.preventDefault();
180+
$e.stopPropagation();
168181
}
169182
}
170183
},

0 commit comments

Comments
 (0)