【问题标题】:External Json with autocomplete jquery带有自动完成 jquery 的外部 Json
【发布时间】:2014-09-23 14:07:11
【问题描述】:
【问题讨论】:
标签:
jquery
json
jquery-ui
autocomplete
【解决方案1】:
查看您的代码,您可以做三件事来获得解决方案:
- 在服务器端处理过滤并在提供过滤结果的端点上调用 ajax 函数
-
如果它是一组静态对象,则在 .tagit() 绑定之外加载 json 并将 json 结果传递给 availableTags 属性。默认情况下会处理过滤。
$("#mytags").tagit({
availableTags: jsonResult, // search.json is loaded into jsonResult
show_tag_url: "/tags/",
singleField: true,
singleFieldNode: $('#submit_tag_names')
});
-
否则,如果出于某种原因更喜欢您的方法 - 在 javascript 端过滤成功结果。将脚本中的成功函数更改为:
success: function(choices) {
var filter = search.term.toLowerCase();
var filteredChoices = $.grep(choices, function(element) {
// Only match autocomplete options that begin with the search term.
return (element.toLowerCase().indexOf(filter) === 0);
});
showChoices(this._subtractArray(filteredChoices, this.assignedTags()));
}
希望对你有帮助