jQuery 错误:无法在初始化之前调用自动完成方法;试图调用方法“销毁”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21549126/
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
Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'destroy'
提问by javacreed
I am using ajax-solr. It works fine with jquery-ui-1.8.*.
我正在使用 ajax-solr。它适用于 jquery-ui-1.8.*。
But when I upgrade it to jquery-1.10.* it gives me this exception:
但是当我将它升级到 jquery-1.10.* 时,它给了我这个例外:
cannot call methods on autocomplete prior to initialization; attempted to call method 'destroy'
不能在初始化之前调用自动完成方法;试图调用方法“销毁”
回答by javacreed
I got the answer to it in the initialization of autocomplete widget...just add
我在自动完成小部件的初始化中得到了答案......只需添加
$(this.target).find('input').autocomplete();
This will initialize the autocomplete widget this happened because in 1.10.*.Jquery has added error messages for usage of functions without proper initialization whereas it was not there in previous versions.
这将初始化发生这种情况的自动完成小部件,因为在1.10.*.Jquery 中添加了错误消息,用于在没有正确初始化的情况下使用函数,而在以前的版本中没有。
回答by Rupert Angermeier
If you want to make sure the autocomplete widget is destroyed, you can check if it was actually initialized by inspecting the CSS classes of the target element, e.g.
如果你想确保自动完成小部件被销毁,你可以通过检查目标元素的 CSS 类来检查它是否真的被初始化,例如
if ($target.hasClass('ui-autocomplete')) {
$target.autocomplete('destroy')
}
回答by Siena
I was getting similar error regarding the widget call,
关于小部件调用,我遇到了类似的错误,
Inside the autocomplete initialization, we were calling its widget function, that was creating the console error.
在自动完成初始化中,我们调用了它的小部件函数,该函数正在创建控制台错误。
In test-result.component.ts,
在 test-result.component.ts 中,
(<any> window).$(testelementId).autocomplete({
……..
(<any> window).$(this).autocomplete('widget').css('width', '100%');
…..
})
So, the easiest fix was to remove that particular call or place it after the initialization and test if that breaks any style. For me, removing the widget call is not doing any harm, so went ahead with that.
因此,最简单的解决方法是删除该特定调用或将其放置在初始化之后,并测试是否会破坏任何样式。对我来说,删除小部件调用不会造成任何伤害,所以继续这样做。