如何使用自动完成(jQuery UI)更改选择事件上的文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8285198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change textbox value on select event with autocomplete (jQuery UI)
提问by el_pup_le
Why won't #input-myBox
clear when I select an item? It seems autocomplete is preventing my .val('')
to work so how can I workaround this?
为什么#input-myBox
在我选择一个项目时不会清除?似乎自动完成阻止了我.val('')
的工作,所以我该如何解决这个问题?
$("#input-myBox").autocomplete({
source: response,
minLength: 1,
select: function(event, ui) {
var selectedObj = ui.item;
$("#input-myBox").appendTo(".foo");
$("#input-myBox").val('');
}
});
回答by el_pup_le
event.preventDefault()
stops autocomplete setting the field.
event.preventDefault()
停止自动完成设置字段。
select: function (event, ui) {
event.preventDefault();
var selectedObj = ui.item;
$("#input-myBox").appendTo(".foo");
$("#input-myBox").val('');
}
回答by kd12
Also, you can use 'return false;' to stop autocomplete setting the field.
此外,您可以使用'return false;' 停止自动完成设置字段。
select: function (event, ui) {
var selectedObj = ui.item;
$("#input-myBox").appendTo(".foo");
$("#input-myBox").val('');
return false;
}
回答by HerrimanCoder
If you just want to substitute the selected value for something else:
如果您只想将选定的值替换为其他值:
select: function( event, ui ) {
ui.item.value = substituteWord(ui.item.value);
}
回答by psynnott
Is it #input-mybox
(lowercase b) or #input-myBox
(uppercase b) ?
是#input-mybox
(小写 b) 还是#input-myBox
(大写 b) ?
This might be your problem :)
这可能是你的问题:)
Edit: rob beat me to it
编辑:抢夺了我
回答by gdoron is supporting Monica
There is inconsistent with your code:
与您的代码不一致:
$("#input-mybox").appendTo(".foo");
$("#input-myBox").val('');
"#input-myBox" || "#input-mybox" ???
“#input-myBox” || “#input-mybox”???