jquery - Select Options from JSON -


i newb when comes json , wanting try write json select option autofiller, not know start.

the way script works using php , mysql fill first set of select options distinct list db table , upon user selection next set of select options autofilled options linked first set. there anyway in json?

sure. lets have simple json:

{ "options": [     { "text":"mytext","value":"myvalue"},     { "text":"mytext2","value":"myvalue2"}    ] } 

then, evaluate javascript:

var options = eval('(' + myjson + ')'); // myjson data variable 

then, create each option in dom (i'll use jquery brevity sake)

var length = options.length;  for(var j = 0; j < length; j++) {     var newoption = $('<option/>');     newoption.attr('text', options[j].text);     newoption.attr('value', options[j].value); // fixed typo     $('#myselect').append(newoption); } 

or similar effect.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -