jQuery UI 自动完成刷新数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4699307/
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
jQuery UI autocomplete refresh data
提问by Upvote
I use jQuery ui autocomplete feature.
我使用 jQuery ui 自动完成功能。
var colors;
$(document).ready(function(){
loadColors();
$('#empf').autocomplete(colors);
}
function loadColors(){
colors = new Array(getNumColor());
//in a loop save the colors to array using colors[i] = ...
}
function addColor(){
...
color[n] = color;
}
When the user enters new color it is saved to the color array. I switch to the autocomplete form but the entered data is not aviable until I refresh the page.
当用户输入新颜色时,它会保存到颜色数组中。我切换到自动完成表单,但在刷新页面之前输入的数据不可用。
Any ideas how to make the new color avialable for autocomplete?
任何想法如何使新颜色可用于自动完成?
回答by Nick Craver
When you update the color, you need to also update the source that autocomplete uses, like this:
function addColor() {
//add colors
$('#empf').autocomplete("option", { source: colors });
}
Here's a sample demo doing this, adding a color and updating the autocomplete source once a second.
这是执行此操作的示例演示,添加颜色并每秒更新一次自动完成源。
回答by Charles Robertson
I've tried Nick Craver's solution, which looks completely logical. Maybe, it is because I am using a URL string rather than an array as the 'source'. Unfortunately his solution does not do a live refresh for the ajax data returned. In order to refresh an ajax data source, I found that the following works:
我试过 Nick Craver 的解决方案,它看起来完全合乎逻辑。也许,这是因为我使用 URL 字符串而不是数组作为“源”。不幸的是,他的解决方案没有对返回的 ajax 数据进行实时刷新。为了刷新一个ajax数据源,我发现以下工作:
element.autocomplete("option","source",url);
element.autocomplete("search");
I think the 'search' method is required for string or ajax URL source types.
我认为字符串或 ajax URL 源类型需要“搜索”方法。