tags:

views:

25

answers:

1

Hi everybody,

I load a page with ajax/jquery, inside it i have some radio one of this may be checked, I'm trying to get value of checked radio, I can not get it on page loading but after it's loaded if a check another radio I can get value. here is example code:

html:

<form id="form1">
<input type="radio" name="a" value="10"> a
<input type="radio" name="a" value="15"> b
<input type="radio" name="a" value="19"> c
</form>

jquery:

$(function(){
var ch=$("input[name='a']:radio:checked");
    $.each(ch, function()
           {
               var chv=$(this).val();
               alert(chv);
           });

$(":radio[name='a']").click(function(){
    var ch=$("input[name='a']:radio:checked");
    $.each(ch, function()
           {
               var chv=$(this).val();
               alert(chv);
           });
});
});

Thanks in advance. ciao h

+1  A: 

I think the problem your running in to is that you're not putting the code in a document.ready() wrapper. You could try doing it like this:

$(document).ready(function(){
 // get value if clicked
 $("input:radio[name='a']").click(function(){
   var value = $(this).val();
   alert(value);
 });

 // get value on load
 var valueOnLoad = $("input[name='a']:radio:checked").val();
 alert(valueOnLoad);

});
Paul Sheldrake
Thanks for answering Paul but when I load the page I receive in the alert undefined instead of the coorect value :( any idea? thanks again ciao h.
haltman
You'll need to add a 'checked="checked"' to one of your inputs otherwise it throws the undefined error in IE. I.E. <input type="radio" name="a" value="10" checked="checked">
Paul Sheldrake
thanks Paul! with your last suggestion I get my solution!
haltman