admin管理员组文章数量:1406943
I have a searchbox on which I initialize typeahead when I click on it, to some data from the server. Depending on the current page, I need to show/hide a footer in the search dropdown.
I went for the approach to destroy the typeahead and then recreate it, according to my needs. In this way, I also refresh the data from server, in case there's something changed.
For destroy I use this this.$("#searchQuery").typeahead('destroy');
right at the top of the function that needs to initialize the typeahead, but the problem is that the menu is not destroyed. If I look at the dom, there are 4-5 or even more typeahead menu created.
So my question is, how can I properly 'reinitialize' a typeahead menu?
I forgot to mention I am using twitter typeahead
I have a searchbox on which I initialize typeahead when I click on it, to some data from the server. Depending on the current page, I need to show/hide a footer in the search dropdown.
I went for the approach to destroy the typeahead and then recreate it, according to my needs. In this way, I also refresh the data from server, in case there's something changed.
For destroy I use this this.$("#searchQuery").typeahead('destroy');
right at the top of the function that needs to initialize the typeahead, but the problem is that the menu is not destroyed. If I look at the dom, there are 4-5 or even more typeahead menu created.
So my question is, how can I properly 'reinitialize' a typeahead menu?
I forgot to mention I am using twitter typeahead
- What is the jquery plugin that you are using? – Krasimir Commented Sep 19, 2013 at 6:31
-
You don't need to reference jQuery via
Window
usingthis.$(...)
you can just do$("#searchQuery").typeahead("destroy");
and that could cause you problems depending on the function that you said you are using, which that line is in... becausethis
may no longer represent theWindow
see developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Pricey Commented Sep 29, 2013 at 23:29
1 Answer
Reset to default 5You don't need to reference jQuery
via this
like you showed here
this.$("#searchQuery")
You should just do
$("#searchQuery").typeahead("destroy");
Without seeing more of an example I would say your problem might be because this
inside your function is being bound to a different object than the one you were expecting.
See: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/this
Your line is inside your function so you can not guarantee that this
will be Window
and you have no need to use it in this case.
If this is not the problem then please provide a full html and javascript example, preferably on JsFiddle.
本文标签: javascriptDestroy typeahead after the menu has been closedStack Overflow
版权声明:本文标题:javascript - Destroy typeahead after the menu has been closed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744333054a2601055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论