Example need for ajax, where on selecting a radio button will dynamically produce drop down
views:
42answers:
2
A:
could you explain yourself a little more?
You need an example where if you click on a radio button and select is showing?
if yes you don't need ajax.
My radio: <input type="radio" name="YOUR_RADIO" onclick="document.getElementById('selectfield').style.display='';" />
<div id="selectfield" style="display:none">
<select><option>option</option></select>
</div>
good luck ...
Mihai Iorga
2010-05-21 11:51:43
+2
A:
If you use or can use jQuery, this is the way I would do it:
Having this HTML:
<input type="radio" name="myradio" value="foo" />
<input type="radio" name="myradio" value="bar" />
<input type="radio" name="myradio" value="baz" />
and assuming you have a page "your_page.php" that returns the list to populate the drop down as JSON, do this:
$("input[@name='myradio']").change(function(){
var selected_value = $("input[@name='myradio']:checked").val();
$.getJSON("your_page.php", { value: selected_value }, populate_dropdown);
});
function populate_dropdown(items) {
// "items" is the ajax-loaded list based on the selected radio button.
// Clear the drop down, populate it and show it if hidden.
}
Dom De Felice
2010-05-21 11:53:13
Ah. `$` how we love you so. Well. People writing libraries love it anyway. If you're going to depend on a third party library then it is worth mentioning which one — especially with as an opaque name as `$` which is used by Prototype and Moo Tools (among others)
David Dorward
2010-05-21 12:27:32
Oops.. I would swear I had read the jquery tag! Really sorry. I updated my answer.
Dom De Felice
2010-05-21 12:34:18
@David - On site named jQueryOverflow, it's obviously jQuery! (Well, the site should be named that...)
Coronatus
2010-05-21 12:38:46
Looking better. I'll give you an upvote if you use the *data* argument of the method instead of the (deprecated) escape function. :)
David Dorward
2010-05-21 12:42:29
Done, thanks for the suggestion :)
Dom De Felice
2010-05-21 12:48:46
Looking rather good now.
David Dorward
2010-05-21 12:52:43