jQuery自动完成,将多个自动完成实例绑定到相同的文本输入
问题描述:
我试图用一个文本字段控制两个jQuery autocomplete
搜索。jQuery自动完成,将多个自动完成实例绑定到相同的文本输入
我可以用下面的代码添加jQuery autocomplete
到输入:
$("search-input").bind("autocompleteselect", jQuery.proxy(function (event, ui) {
//List element select callback
}, this)).autocomplete({
appendTo:"#result-list-1",
source: function (request, response) {
$.ajax({
url://some rest url,
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function (data) {
alert("fb sucess");
response($.map(data.data, function (item) {
//data mapping instructions
}));
},
});
}
}).data("autocomplete")._renderItem = jQuery.proxy(function (ul, item) {
return $("<li></li>").data("item.autocomplete", item).append("some html to append").appendTo(ul);
})
}
当我尝试这第二次与其他参数的自动完成功能,它的工作原理应用到$("search-input")
但撤销原有的功能。
任何人可以建议一种方法来设置第二个自动完成与撤消第一个?
答
// getter
var oldsource = $(".selector").autocomplete("option", "source");
var newsource = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]; //A new json object
//Merge it with old source
$.merge(oldsource, newsource);
// setter
$(".selector").autocomplete("option", "source", oldsource);
这是有趣的,但我不知道如何将它应用到我的数据源是AJAX调用的情况。我不知道如何合并两个AJAX调用。 – 2013-03-14 08:47:48
执行另一个Ajax调用并将重新调用的数据合并到成功事件中的旧资源。 – 2013-03-14 11:01:45
$阿贾克斯({ 网址://休息一下网址, 数据类型: “JSONP”, 数据:{ 要素类: “P”, 风格: “完全”, maxRows进行:12, name_startsWith:请求。 (数据){ // getter var oldsource = $(“.selector”).autocomplete(“option”,“source”); var newsource = data; //一个新的json对象 //将其与旧来源合并 $ .merge(oldsource,newsource); // setter $(“.selector”).autocomplete(“option”,“source”,oldsource); })); }, }); – 2013-03-14 11:07:09