开发者

jQuery Autocomplete: how to refresh list?

I have a form with a variable number of autocomplete fields that use the same select list. These fields are added and removed as needed. Whenever a parameter on the page is changed, I (try to) update the list for all the fields by first calling unbind() then autocomplete() with a new parameter added to the url.

$('input.foo').unbind().autocomplete(url?new_param=bar);

The problem is that unbind() does not seem to be unbinding. W开发者_高级运维hen I type in the input field, it fires off the entire history of autocomplete events.

I also tried flushCache to no avail.

How do I clear out the old events?

Thanks.


$(target).autocomplete("destroy");


Try this

$ ('input.foo').unbind('.autocomplete').autocomplete ('url?new_param=bar') ;


The autocomplete plugin adds a function called unautocomplete() to remove autocomplete() from fields that have it, so it should work if change your code to:

$('input.foo').unautocomplete().autocomplete('url?new_param=bar');

This unautocomplete() function should remove all of the old events.


jQuery provides flushCache() method (http://docs.jquery.com/Plugins/Autocomplete) to remove the contents already matched through auto-complete.

For example, if the html element used is <input type="text" id="txtElement"/> and auto-complete is associated with it. The flushCache can be given as follows: $("#txtElement").flushCache(); and this would solve the problem.


If you are using options then add cacheLength and set it to 1. This setting will not store any of returned records. Trust me, it has made my life better ... for all of 5 minutes.


I think your problem is that you don't have a form enclosing the autocomplete input field. If you look in the code of jquery.autocomplete.js it does $(input.form).unbind(".autocomplete"); on unautocomplete(), which means the input must live inside a form tag in order to work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜