jQuery自动完成:如何从列表(键盘)中强制选择 [英] JQuery autocomplete: how to force selection from list (keyboard)
问题描述
我正在使用JQuery UI自动完成功能.一切都按预期工作,但是当我使用键盘上的上/下键循环时,我注意到文本框按预期充满了列表中的项目,但是当我到达列表末尾并再次按下向下箭头时,时间,我输入的原始术语就会显示出来,基本上可以使用户提交该条目.
I am using JQuery UI autocomplete. Everything works as expected, but when I cycle with the up/down keys on the keyboard, I notice that the textbox is filled with items in the list as expected, but when I reach the end of the list and hit the down arrow one more time, the original term that I typed shows up, which basically allows the user to submit that entry.
我的问题:是否有一种简单的方法将选择范围限制为列表中的项目,并从键盘选择中删除输入中的文本?
My question: Is there a simple way to limit the selection to the items in the list, and remove the text in the input from the keyboard selection?
例如:如果我有一个包含{'Apples (AA)', 'Oranges (AAA)', 'Carrots (A)'}
的列表,则如果用户键入'app',我将自动选择列表中的第一项(此处为'Apples(AA)'),但是如果用户按下向下箭头,"app"将再次显示在文本框中.我该如何预防?
eg: if I have a list that contains {'Apples (AA)', 'Oranges (AAA)', 'Carrots (A)'}
, if the user types 'app', I will automatically select the first item in the list ('Apples (AA)' here), but if the user presses the down arrow, 'app' shows up again in the textbox. How can I prevent that?
谢谢.
推荐答案
对于强制选择,可以使用更改"自动完成
for force selection, you can use "change" event of Autocomplete
var availableTags = [
"ActionScript",
"AppleScript"
];
$("#tags").autocomplete({
source: availableTags,
change: function (event, ui) {
if(!ui.item){
//http://api.jqueryui.com/autocomplete/#event-change -
// The item selected from the menu, if any. Otherwise the property is null
//so clear the item for force selection
$("#tags").val("");
}
}
});
这篇关于jQuery自动完成:如何从列表(键盘)中强制选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!