I have already talked about returning json data to the foreground in the background and traversing the json data in the foreground.
Here we will talk about creating JSON data directly in JS, and then traversing and using it~
The creation code is as follows: (JSON object is created)
var YearSelect = {}; var Year = 2014; var DateOption; for (var i = Year; i < Year + 12; i++) { DateOption = {'Year':i, 'Month':i-Year+1}; / alert(DateOption.Year) YearSelect[i] = DateOption; }
Here is a JSON object created, including year and month data.
Why I created JSON objects is because I am familiar with JSON objects. What the php background returns is also a json object.
json object has no length attribute~~
So to traverse:
for(var key in YearSelect){ alert(YearSelect[key].Year); alert(YearSelect[key].Month); }
That’s it~
Remember to distinguish between json objects and arrays~ otherwise it will always be undenfined