jQuery 自动完成:如何刷新列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1581061/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:46:34  来源:igfitidea点击:

jQuery Autocomplete: how to refresh list?

jquerybindingjquery-autocomplete

提问by cinematic

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.

我有一个表单,其中包含可变数量的使用相同选择列表的自动完成字段。根据需要添加和删除这些字段。每当页面上的参数发生更改时,我(尝试)通过首先调用unbind()然后autocomplete()将新参数添加到 url来更新所有字段的列表。

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

The problem is that unbind() does not seem to be unbinding. When I type in the input field, it fires off the entire history of autocomplete events.

问题是 unbind() 似乎没有解除绑定。当我在输入字段中输入时,它会触发自动完成事件的整个历史记录。

I also tried flushCache to no avail.

我也试过flushCache无济于事。

How do I clear out the old events?

如何清除旧事件?

Thanks.

谢谢。

回答by Alvin Gore

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

回答by Rubasu

Try this

尝试这个

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

回答by Deeksy

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:

自动完成插件添加了一个名为 unautocomplete() 的函数来从具有它的字段中删除 autocomplete(),因此如果将代码更改为:

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

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

这个 unautocomplete() 函数应该删除所有旧事件。

回答by learningloop

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

jQuery 提供了flushCache() 方法( http://docs.jquery.com/Plugins/Autocomplete) 来移除已经通过自动完成匹配的内容。

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.

例如,如果使用的 html 元素是<input type="text" id="txtElement"/>并且自动完成与它相关联。可以如下给出flushCache:$("#txtElement").flushCache();这将解决问题。

回答by Edwin

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.

如果您使用选项,则添加 cacheLength 并将其设置为 1。此设置不会存储任何返回的记录。相信我,它让我的生活变得更好......所有 5 分钟。

回答by Edwin

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.

我认为您的问题是您没有包含自动完成输入字段的表单。如果您查看 jquery.autocomplete.js 的代码,它会执行 $(input.form).unbind(".autocomplete"); 在 unautocomplete() 上,这意味着输入必须位于表单标签内才能工作。