我想解析一个 JSON 对象并从中创建下拉列表。将有六个下拉菜单 -
productType reportNames startDate startMonth endDate endMonth
我能够创建第一个下拉列表“productType”,但是当我尝试创建其他下拉列表时,它会显示一些未定义的错误消息。
这是我的代码 -
HTML
<select id="product_type"></select>
<select id="report_type"></select>
<select id="startDate"></select>
<select id="startMonth"></select>
<select id="endDate"></select>
<select id="endMonth"></select>
Javascript
var jsonString = '[{"productType": "ABC","reportNames": ["Report1",{"startDate": ["2010","2011"],"startMonth": ["May","June"],"endDate":["2010","2011"],"endMonth": ["May","June"]},"Report 2",{"startDate": ["2010","2011"],"startMonth": ["May","June"],"endDate": ["2010","2011"],"endMonth": ["May","June"]}]},{"productType": "XYZ","reportNames": ["Report 3",{"startDate": ["2010","2011"],"startMonth": ["May","June"],"endDate": ["2010","2011"],"endMonth": ["May","June"] },"Report 4",{"startDate": ["2010","2011"], "startMonth": ["May", "June" ],"endDate": ["2010","2011"],"endMonth": ["May","June"]}]}]';
var myData = JSON.parse(jsonString);
var $product_type = $('#product_type');
var $report_type = $('#report_type');
$.each(myData, function () {
$('<option>' + this.productType + '</option>').appendTo($product_type);
});
$.each(myData, function () {
$('<option>' + this.productType[0].reportNames + '</option>').appendTo($report_type);
});
基本上我想创建六个这样的下拉菜单:
产品类型 - ABC、XYZ
报告名称 - 报告 1、报告 2
开始日期 - 2010, 2011
startMonth - 五月,六月
结束日期 - 2010, 2011
endMonth - 五月,六月
我正在使用 jQuery 来解析 JSON 对象。这是演示 - http://jsfiddle.net/Lnv9d/3/