我可以从服务返回任何JSON。基于JSON,我需要动态创建SelectBoxes并填入其中的值。
EG -
JSON = {
"Level": [{
"Product": [{
"ID": "ID1",
"Brand": "Brand2",
"Type": "Type3",
"Line": "Line4",
"Family": "Family5"
}],
"Location": [{
"City": "City1",
"State": "State2",
"Region": "Region3",
"Country": "Country4"
}],
"Time": [{
"Day": "Day1",
"Week": "Week2",
"Month": "Month3",
"Quarter": "Quarter4",
"Year": "Year5"
}]
}]
}
在这种情况下,将创建3个主选择框,其下方有子选择框。喜欢 - OneMain SelectBox - 时间,低于时间5个选择框, 第二个SelectBox - 位置,在另外4个选择框之下,依此类推。
如何使其动态达到这样的水平是完全无能为力的。
答案 0 :(得分:3)
试试这个,
var level=JSON['Level'][0];
for(var pr in level){
var $select =$('<select/>');
$select.append('<option>'+pr+'</option>');
key=level[pr][0];
for(k in key){
$select.append('<option>'+key[k]+'</option>');
}
$select.appendTo('body');
}
答案 1 :(得分:0)
嗨,我认为这就是你想要的example,我使用Jquery
var JSONS = {
"Level": [{
"Product": [{
"ID": "ID1",
"Brand": "Brand2",
"Type": "Type3",
"Line": "Line4",
"Family": "Family5"
}],
"Location": [{
"City": "City1",
"State": "State2",
"Region": "Region3",
"Country": "Country4"
}],
"Time": [{
"Day": "Day1",
"Week": "Week2",
"Month": "Month3",
"Quarter": "Quarter4",
"Year": "Year5"
}]
}]
}
$(document).ready(function(){
$.each(JSONS.Level[0],function(key,val){
var currentSection = val[0];
var selectEle=$('<select id="'+key+'"></select>');
$.each(currentSection,function(k,va){
var op = "<option>"+va+"</option>";
selectEle.append(op);
});
$('#selectWrap').append(selectEle);
});
});