Parsing JSON Object in jQuery and create drop downs -
i want parse json object , create drop downs out of it. there 6 drop downs -
producttype reportnames startdate startmonth enddate endmonth
i able create first drop down 'producttype' when trying create others shows me undefined error message.
here code -
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);
});
basically want create 6 drop downs this:
producttype - abc, xyz
reportnames - report1, report2
startdate - 2010, 2011
startmonth - may, june
enddate - 2010, 2011
endmonth - may, june
i using jquery parse json object. here demo - http://jsfiddle.net/lnv9d/3/
you accessing json incorrectly second dropdown.
this.producttype
string, not array, cannot call this.producttype[0].reportnames
if change this.reportnames[0]
may looking for.
edit: here new fiddle handles showing , hiding options based on selected. use optimization, works.
Comments
Post a Comment