jQuery 添加到元素后如何删除自动完成功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3102262/
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 remove an autocomplete once added to an element?
提问by P Coder
After I add a autocomplete to an element like:
在向元素添加自动完成功能后,例如:
commObj=$("#communityName").autocomplete("auto_commName_backend.php",
{'onItemSelect': handleItemSelect}, {'extraParams': 'ndo' + ndo});
Is there a way I could disable it or remove the autocomplete for the element dynamically?
有没有办法可以禁用它或动态删除元素的自动完成功能?
I am using modified jquery autocomplete plugin from Dylan Verheul. http://www.pengoworks.com/workshop/jquery/autocomplete_docs.txt
我正在使用 Dylan Verheul 的修改过的 jquery 自动完成插件。 http://www.pengoworks.com/workshop/jquery/autocomplete_docs.txt
回答by Matloob Ali
You can remove the autocomplete behaviour by using following line of code:
您可以使用以下代码行删除自动完成行为:
if (jQuery(control).data('autocomplete')) {
jQuery(control).autocomplete("destroy");
jQuery(control).removeData('autocomplete');
}
回答by Paglian
The easiest and safest way I found is:
我发现的最简单和最安全的方法是:
$(control).autocomplete({source: []});
Alternatively, as mentioned before this also works:
或者,如前所述,这也有效:
$(control).autocomplete("destroy");
If you are sure your control has an autocomplete handler set.
如果您确定您的控件设置了自动完成处理程序。
回答by Vineeth
In the latest version of jQuery UI(1.12), $(control).data('autocomplete')
isn't returning true even when the autocomplete data is present.
在最新版本的 jQuery UI(1.12) 中,$(control).data('autocomplete')
即使存在自动完成数据,也不会返回 true。
Instead, a check for the presence of ui-autocomplete-input
class returned true.
相反,检查ui-autocomplete-input
类的存在返回 true。
The following should suffice, I believe.
我相信以下内容就足够了。
if ($(control).hasClass('ui-autocomplete-input')) {
$(control).autocomplete("destroy");
}
回答by Jared
Disable autocomplete by element ID:
按元素 ID 禁用自动完成:
$("#elementId").autocomplete({
disabled: true
});
回答by Gary
I just tried $('#').autocomplete = null; and it works. I was allowing name change of the fly and the result wouldnt show, now it does. The old unchanged name would show before now the new one does
我刚试过 $('#').autocomplete = null; 它有效。我允许更改苍蝇的名称,结果不会显示,现在可以了。旧的未更改名称会显示在新名称之前
回答by Pointy
That plugin wires itself up to the <input>
pretty tightly. If it were my page, I think I'd just have twotags with the same "name", one with autocomplete and one without. When I wanted to toggle between them, I'd make one disabled and hidden, and enable/show the other. (editoh and also make sure the values of the fields get copied back and forth when they switch)
该插件将自身与<input>
非常紧密的连接在一起。如果是我的页面,我想我只有两个具有相同“名称”的标签,一个具有自动完成功能,一个没有。当我想在它们之间切换时,我会禁用和隐藏一个,并启用/显示另一个。(编辑哦,还要确保字段的值在切换时来回复制)
回答by Rob Allen
If you add a class like, say "EnableAC" to your element, you could target that class for autocomplete. When you complete the autocomplete and want to disable it, you remove that class.
如果您添加一个类,例如对您的元素说“EnableAC”,您可以针对该类进行自动完成。当您完成自动完成并想要禁用它时,您删除该类。
Why would do you want to disable the feature though? It may confuse your users if they want to change their selection and the autocomplete doesn't fire.
你为什么要禁用该功能?如果您的用户想要更改他们的选择并且自动完成不会触发,这可能会使他们感到困惑。
回答by Brian Scott
I've just had a brief look at the js file for the plugin.
我刚刚简要地查看了插件的 js 文件。
There appears to be a javascript line (number 21) during the initialisation of the jquery.autocomplete.js plugin that sets up the originating source input as:
在 jquery.autocomplete.js 插件的初始化过程中似乎有一个 javascript 行(编号 21),它将原始源输入设置为:
input.autocompleter = me;
Without testing, maybe you could simply then call the following to remove the link between the plugin and the original input?
如果没有测试,也许您可以简单地调用以下内容来删除插件和原始输入之间的链接?
$('#<your input id>').autocompleter = null;