jQuery 如何清除自动完成选定的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8867604/
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 clear autocomplete selected values?
提问by Jér?me Verstrynge
I have a page with several JQuery autocompletes. I have implemented a button to clear all selected values, but it does not work... I have tried to set $(...).text("");
but it does not work. Firebugs fails on the line and does not throw any error message. It quits the function.
我有一个包含多个 JQuery 自动完成功能的页面。我已经实现了一个按钮来清除所有选定的值,但它不起作用......我试图设置$(...).text("");
但它不起作用。Firebugs 失败就行,并且不会抛出任何错误消息。它退出该功能。
What is the right way to clear the selected value of JQuery autocompletes, from code?
从代码中清除 JQuery 自动完成的选定值的正确方法是什么?
采纳答案by Hemesh Singh
Try using $(...).attr("value","");
尝试使用 $(...).attr("value","");
Hope it works!! Sorry if i have misunderstood the question.
希望它有效!!对不起,如果我误解了这个问题。
回答by yarg
You should use:
你应该使用:
$('selector').autocomplete('close').val('');
This will also reset the current search of the autocomplete (in addition to clearing the input).
这也将重置自动完成的当前搜索(除了清除输入)。
回答by Shobhit Sharma
You should use this one, it'll work
你应该使用这个,它会起作用
$("selector").autocomplete({
source: ,
delay: 200,
minLength: 3,
select: function (event, ui) {
ui.item.value = ""; // it will clear field
return false;
}
});
回答by Avinash
Try this,
尝试这个,
$("selector").autocomplete({
source: ,
delay: 200,
minLength: 3,
select: function (event, ui) {
$(this).val() = "";
return false;
}
});