that is a different issue than the ajax thing. but, it's also a common thing.. :)

$("input[name='myradio']:checked").val()

That should do it. Get the value of the checked radio option... I changed it to not use an ID, cuz chances are you are you are using a name and not an ID...

Failing that, you could extend things a little and loop over each of the radios:

$("input[name='myradio']").each( function () {
  if ($(this).attr("checked") == true) {
    radioValue = $(this).val();
    //do something with the value
    // ...

    //no need to loop to the next item,
    //so return false to end the .each()
    return false;
  }
});

The first approach is preferable, I'd think - less looping involved. But this second approach shows an expanded way to do the same thing, which is sometimes needed when doing something a little more non-simple.

HTH

Shawn

lionel28 wrote:

hummm,

not that simple. It's multiple radios with multiple values
so how to get id and value of selected one?




lionel28 wrote:
Thanks for replying.
I am going to try your code.


Shawn Grover wrote:

in short, no. Not that I know of. I mean no plugin per se. The code/script is simple enough though...

$("#myradio").click( function () {
   $.ajax({
     url: "somepage.php",
     data: "id=somevalue",
     dataType: "json",
     success: function (results) {
       var opts = "";
       for (x=0; x < results.length; x++) {
         opts += "<option>" + results[x] + "</option>";
       }
       $("#mySelect").empty().append(opts);
     }
   });
});

that's kinda rough, but should get the point across. The results you get back from the ajax call can be whatever you want - a new HTML definition of the select element, a json array of objects, a simple array of text values (as hinted at in the sample code), or even xml. Whatever you pick for the results format dictates what you have to do with the results to get it into your select list.

The flexibility and diversity of the approaches here are probably why there is no (known) plugin. Yet the code is simple enough to slap together in less than 5 minutes.

HTH.

Shawn

lionel28 wrote:
Hello,

Is there a script that allows to get values to populate a select box via
ajax when a radio button is clicked?

Thank you.



Reply via email to