我正在循环使用一些JSON并填充所有发布者的下拉列表(不论类型)。
我的代码是:
//Populate the Publisher
function populatePublishers(){
var myData = JSON.parse($RetrievedData);
$(myData.genre).each(function (i) {
$(this.publishers).each(function (i) {
getUnique(this);
});
});
};
//find only unique publishers
function getUnique(option) {
var optionExists = $("#comboPublisher").find("option[value='" + option.publisher + "']").length > 0;
if (!optionExists) {
$("#comboPublisher").append($("<option/>", {
val: option.publisher,
html: option.publisher
}));
$('#comboPublisher').selectpicker("refresh");
}};
我现在要做的是返回选定发布商的游戏。因此,comboPublisher框显示所有发布者,用户选择说暴雪,下一个下拉列表将包含星际争霸和暗黑破坏神。
我试过这个:
function populateGames(){
var myData = JSON.parse($RetrievedData);
$(myData.genre).each(function (i) {
$(this.publishers).each(function (i) {
$(this.games).each(function(i){
uniqueOptions2(this);
});
});
})};
但是它会返回所有游戏,而不仅仅是选定发布者的游戏。我有什么想法可以实现它吗?
我的Json采用以下格式:
{"genres": [{
"genre": "RPG",
"publishers": [{
"publisher": "Square",
"games": [{
"game": "FFX",
"rating": [
12, 15
]
}]
}, {
"publisher": "Blizzard",
"games": [{
"game": "Starcraft",
"rating": [
10, 12
]
}, {
"game": "Diablo",
"rating": [
15
]
}, ]
}]
}, {
"genre": "Action",
"publishers": [{
"publisher": "EA",
"games": [{
"game": "COD",
"rating": [
18, 20
]
}, {
"game": "Titanfall",
"rating": [
18
]
}]
}]
}
}
默认情况下,选择发布者时,应为游戏和评级选择所有