使用jQuery Autocomplete返回多个值
我试图使用jQuery自动完成小部件从SpotifyAPI返回track“href”值。这是我的密码:使用jQuery Autocomplete返回多个值,jquery,jquery-ui,api,autocomplete,spotify,Jquery,Jquery Ui,Api,Autocomplete,Spotify,我试图使用jQuery自动完成小部件从SpotifyAPI返回track“href”值。这是我的密码: $(function() { $("#spotify_song_search").autocomplete({ source: function(request, response) { $.get("http://ws.spotify.com/search/1/track.json", { q: requ
$(function() {
$("#spotify_song_search").autocomplete({
source: function(request, response) {
$.get("http://ws.spotify.com/search/1/track.json", {
q: request.term
}, function(data) {
response($.map(data.tracks, function(el, ui) {
return el.name;
}));
});
},
select: function(el, ui) {
alert(ui.item.href);
}
});
});
响应的URL:
在其当前状态下,它会发出警报[对象]
。要返回href值,我需要做什么?这样做怎么样:
$(函数(){
$(“#spotify_song_search”).autocomplete({
来源:功能(请求、响应){
$.get(”http://ws.spotify.com/search/1/track.json", {
问:请求.期限
},函数(数据){
响应($.map(data.tracks,函数(项)){
返回{label:item.name,track:item};
}));
});
},
选择:功能(el、ui){
控制台日志(ui);
$(“#track”).attr(“href”,ui.item.track.href).text(“Listen”);
}
});
});
另外,这么多的代码实际上让我可以搜索曲目并播放它们,这让我大吃一惊。互联网……太棒了。:) 下面是我使用的一个示例,我在调用时返回一些值,以便在页面的其他地方使用
$("#SearchField").autocomplete({
source: function (request, response) {
var term = request.term;
if (term in entityCache) {
response(entityCache[term]);
return;
}
if (entitiesXhr != null) {
entitiesXhr.abort();
}
$.ajax({
url: actionUrl,
data: request,
type: "GET",
contentType: "application/json; charset=utf-8",
timeout: 10000,
dataType: "json",
success: function (data) {
response($.map(data, function (item) {
return { label: item.SchoolName, value: item.EntityName, id: item.EntityID, code: item.EntityCode };
}));
}
});
},
minLength: 3,
select: function (event, result) {
$("#EntityID").val(result.item.id);
$("#Code").val(result.item.code);
}
});
它也有一个缓存机制:-)你能粘贴响应吗?这是响应的url:对不起,请确保它是json链接为什么不记录它并查看对象中的内容?因为我不知道该怎么做lol我将进行谷歌搜索
$("#SearchField").autocomplete({
source: function (request, response) {
var term = request.term;
if (term in entityCache) {
response(entityCache[term]);
return;
}
if (entitiesXhr != null) {
entitiesXhr.abort();
}
$.ajax({
url: actionUrl,
data: request,
type: "GET",
contentType: "application/json; charset=utf-8",
timeout: 10000,
dataType: "json",
success: function (data) {
response($.map(data, function (item) {
return { label: item.SchoolName, value: item.EntityName, id: item.EntityID, code: item.EntityCode };
}));
}
});
},
minLength: 3,
select: function (event, result) {
$("#EntityID").val(result.item.id);
$("#Code").val(result.item.code);
}
});