Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click or change event on radio using jquery

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...

like image 625
hguser Avatar asked Mar 02 '11 10:03

hguser


People also ask

How do you checked radio button is changed in jQuery?

Now, to detect when that radio box is changed, you can easily call the jQuery 'change' event inside your jQuery document ready function: $(document). ready(function(){ $('#my_radio_box'). change(function(){ alert('Radio Box has been changed!

What is the event for radio button?

A radio button fires the change event after you select it. How it works: First, register an event handler to the change event of the body . When a radio button is clicked, its change event is bubbled to the body.


1 Answers

This code worked for me:

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

http://jsfiddle.net/3q29L/

like image 185
alexl Avatar answered Sep 30 '22 10:09

alexl