im使用jquery和php构建一个抽奖系统。我现在正在工作。我唯一的问题是来自我的Ajax的json数据,我从json返回了多维数组。我想将其转换为数组,以便可以在json中获取名称和ID。
{"participants":[{"name":"Acad-HS Male","id":"1"},{"name":"MGMT Female","id":"2"},{"name":"Acad-HS Female","id":"3"},{"name":"Acad-College Male","id":"7"},{"name":"Acad-College Female","id":"8"},{"name":"Acad-Elem Female","id":"9"},{"name":"Acad-HS Female","id":"10"},{"name":"NAP-Coll Male","id":"11"}]}
上面是我的json
var names = [];
var index = -1, looper, lastwinner;
$(document).on("click","#startgpraffle", function(){
$.ajax({
type: "POST",
url: baseUrl+"/home/getgrandprizeparticipants/",
//dataType: 'json',
cache: false,
success: function(data){
$.each(data.participants, function(i, value) {
names.push(value['name']);
//I WANT MY ID ALSO PUSHED IN THE ARRAY
});
}
});
(function __cycle(){
var name = names[++index % names.length];
$("#winnername").text(name);
looper = setTimeout(__cycle, 500);
})();
});
$(document).on("click","#gpwinner", function(){
setTimeout(function(){
var name = names[rand(0, names.length - 1)];
clearTimeout(looper);
$("#winnername").text(name);
//REMOVE FROM LIST
$.ajax({url: baseUrl+"/home/savegpwinner",data: {'name':name},success: function(data){
//alert(data);
}
});
}, rand(350, 700));
});
在我单击gpwinner之后,我想基于该数组获取Winning NAME的ID。预先感谢