javascript - JSON structure to populate options for SELECT -
javascript - JSON structure to populate options for SELECT -
in question asked here, see next code can used adding options select
function getresults(str) { $.ajax({ url:'suggest.html', type:'post', data: 'q=' + str, datatype: 'json', success: function( json ) { $.each(json, function(i, optionhtml){ $('#myselect').append(optionhtml); }); } }); };
can please tell me, format of json should be, on php server side. like, how should created, so parsed correctly "$('#myselect').append(optionhtml);" ??
that code parse json, array of strings markup.
[ "<option value='...</option>", "<option value='...</option>", "<option value='...</option>", ... ]
this pretty much defeats purpose of json though.
it have been improve if json contained data:
{ "text1" : "value1", "text2" : "value2", "text3" : "value3", ... }
and code parse , create elements you. way, json lighter.
success: function( json ) { var myselect = $('#myselect') $.each(json, function(key,val){ $('<option/>',{ value : val //i method of making elements improve }) //because string concatenation messy .text(key) .appendto(myselect); }); }
javascript html json
Comments
Post a Comment