click or change event on radio using jquery

hguser picture hguser · Mar 2, 2011 · Viewed 203.9k times · Source

I have some radios in my page,and I want to do something when the checked radio changes,however the code does not work in IE:

$('input:radio').change(...);

And after googling,people suggest use the click instead. But it does not work.

This is the example code:

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <script type="text/javascript">
            $('document').ready(
                function(){
                    $('input:radio').click(
                        function(){
                            alert('changed');   
                        }
                    );  
                }
            );

        </script>
    </head>
    <body>
        <input type="radio" name="testGroup" id="test1" />test1<br/>
        <input type="radio" name="testGroup" id="test2" />test2<br/>
        <input type="radio" name="testGroup" id="test3" />test3</br>
    </body>
</html>

It also does not work in IE.

So I want to know what is going on?

Also I am afraid if it will retrigger the change event if I click a checked radio?

UPDATE:

I can not add comment,so I reply here.

I use IE8 and the link Furqan give me also does not work in IE8. I do not know why...

Answer

alexl picture alexl · Mar 2, 2011

This code worked for me:

$(function(){

    $('input:radio').change(function(){
        alert('changed');   
    });          

});

http://jsfiddle.net/3q29L/