jQuery的自动完成与JSON响应 [英] jquery autocomplete with json response
本文介绍了jQuery的自动完成与JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即时得到的JSON响应,但是这不会解析JSON响应。什么M我做错了吗?我could'nt找到DOC任何 http://docs.jquery.com/Plugins/Autocomplete
$(#用户允许的)。自动完成(/人/ following.json,{
宽度:320,
//最大:4,
亮点:假的,
滚动:真实,
scrollHeight属性:300,
formatItem:功能(响应,我,最大值){
的console.log(响应);
的console.log(响应['项目']);
的console.log(response.items);
回到我+/+ MAX +:\\+ response.status_ code +\\[+ response.status_description +]; //返回< IMG SRC =图片/+价值+'/>中+ value.split(。)[0];
},
formatResult:功能(响应){
//返回value.split(。)[0];
返回response.status_description;
}
});
解决方案
$(#用户允许的)。自动完成(/人/ following.json,{
宽度:320,
数据类型:JSON,
亮点:假的,
滚动:真实,
scrollHeight属性:300,
解析:功能(数据){
VAR阵列=新的Array();
对于(VAR I = 0; I< data.items.length;我++){
数组[array.length] = {数据:data.items [I],值:data.items [I],结果是:data.items [I] .username};
}
返回数组;
},
formatItem:函数(行){
变量名称='';
如果(row.first_name&安培;&安培; row.last_name)
名='('+ row.first_name +','+ row.last_name +)';
否则,如果(row.first_name)
NAME ='('+ row.first_name +')';
否则,如果(row.last_name)
NAME ='('+ row.last_name +')'; 返回row.username +''+名;
}
});
检查数据类型,同时解析选项。
im getting response in json, but this wont parse the json response. what m i doing wrong? i could'nt find anything on doc http://docs.jquery.com/Plugins/Autocomplete
$("#users-allowed").autocomplete("/people/following.json", {
width: 320,
//max: 4,
highlight: false,
scroll: true,
scrollHeight: 300,
formatItem: function(response, i, max) {
console.log(response);
console.log(response['items']);
console.log(response.items);
return i + "/" + max + ": \"" + response.status_code + "\" [" + response.status_description + "]";
//return "<img src='images/" + value + "'/> " + value.split(".")[0];
},
formatResult: function(response) {
//return value.split(".")[0];
return response.status_description;
}
});
解决方案
$("#users-allowed").autocomplete("/people/following.json", {
width: 320,
dataType: 'json',
highlight: false,
scroll: true,
scrollHeight: 300,
parse: function(data) {
var array = new Array();
for(var i=0;i<data.items.length;i++) {
array[array.length] = { data: data.items[i], value: data.items[i], result: data.items[i].username };
}
return array;
},
formatItem: function(row) {
var name = '';
if (row.first_name && row.last_name)
name = '('+row.first_name+', '+row.last_name+')';
else if (row.first_name)
name = '('+row.first_name+')';
else if (row.last_name)
name = '('+row.last_name+')';
return row.username+' '+name;
}
});
check dataType and parse option.
这篇关于jQuery的自动完成与JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文