Fumigator
04-03-2009, 05:20 PM
I am noob to jQuery and JSON. Just found out about JSON actually. Wow lots of fun stuff there.
SO! I love jQuery's plugin library but I could not find a plugin that will "ajax" load a listbox using <optgroup> tags. I figure I can just use getJSON() to retrieve the data from my PHP script, right? So I code up a PHP script to return something that looks like:
{"optgroup": {"o1" : "option1", "o2": "option2", "o3": "option3"},
"optgroup": {"p1" : "optionp1", "p2": "optionp2", "p3": "optionp3"}
}
Which should be returned as a JSON object in theory. And when I alert(returnObj), sure enough, it shows me [object].
SO THEN, I figure I can:
jQuery.each(returnObj, function(i, val) {
if (val == 'optgroup') {
//create <optgroup> tag in the DOM
}
});
I sort of stall out at this point... any direction would be helpful.
Secondary question, how do I structure the JSON syntax so as to include a label with the optgroup? Like this?
{"optgrouplabel": "label1",
"optgroup": {"o1" : "option1", "o2": "option2", "o3": "option3"},
"optgropulabel": "label2",
"optgroup": {"p1" : "optionp1", "p2": "optionp2", "p3": "optionp3"}
}
SO! I love jQuery's plugin library but I could not find a plugin that will "ajax" load a listbox using <optgroup> tags. I figure I can just use getJSON() to retrieve the data from my PHP script, right? So I code up a PHP script to return something that looks like:
{"optgroup": {"o1" : "option1", "o2": "option2", "o3": "option3"},
"optgroup": {"p1" : "optionp1", "p2": "optionp2", "p3": "optionp3"}
}
Which should be returned as a JSON object in theory. And when I alert(returnObj), sure enough, it shows me [object].
SO THEN, I figure I can:
jQuery.each(returnObj, function(i, val) {
if (val == 'optgroup') {
//create <optgroup> tag in the DOM
}
});
I sort of stall out at this point... any direction would be helpful.
Secondary question, how do I structure the JSON syntax so as to include a label with the optgroup? Like this?
{"optgrouplabel": "label1",
"optgroup": {"o1" : "option1", "o2": "option2", "o3": "option3"},
"optgropulabel": "label2",
"optgroup": {"p1" : "optionp1", "p2": "optionp2", "p3": "optionp3"}
}