选择后清除 Jquery 自动完成中的文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11607938/
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
Clear text box in Jquery Autocomplete after selection
提问by user1163513
I want to clear the textbox having Jquery Autocomplete once the user has made the selection.
一旦用户做出选择,我想清除具有 Jquery 自动完成功能的文本框。
I tried to clear the field by :
我试图通过以下方式清除该字段:
select: function(event, ui) {
$(this).val('');
}
But this is not working.I am using jquery-1.6.4 and jquery-ui-1.8.16.
但这不起作用。我正在使用 jquery-1.6.4 和 jquery-ui-1.8.16。
Any Ideas?
有任何想法吗?
回答by Frédéric Hamidi
The selectevent occurs before the field is updated. You will have to cancel the event to avoid the field being updated after you have cleared it:
在选择的字段更新之前发生的事件。您必须取消该事件以避免在清除该字段后更新该字段:
select: function(event, ui) {
$(this).val("");
return false;
}
Update:As T.J. pointed out below, it's slightly faster to update the value
DOM property directly instead of going through val():
更新:正如 TJ 在下面指出的那样,value
直接更新DOM 属性而不是通过val()会稍微快一点:
select: function(event, ui) {
this.value = "";
return false;
}
回答by machineaddict
I'm using jQuery UI version 1.8.23 and $(this).val("");
on select
event hasn't worked for me, but this did:
我正在使用 jQuery UI 版本 1.8.23 并且$(this).val("");
on select
event 对我不起作用,但是这样做了:
$("selector").autocomplete({
// ...
close: function(el) {
el.target.value = '';
}
});
回答by Mayank Pathak
Try
尝试
select: function(event, ui) {
input.value='';
}
回答by InGeek
You may also try this:
你也可以试试这个:
$("#your_autocomplete_sub_listview_id").children().addClass("ui-screen-hidden");
which will hide all the autocomplete filtered list upon selecting it.
这将在选择它时隐藏所有自动完成过滤列表。