Change of Behavior of jQuery Starting with 1.9.0 -
the following code displays alert boxes containing "radio1 selected", "radio2 selected" or "radio3 selected" in response click events on radio buttons when version 1.8.3 of jquery used. versions after displays "none selected". ideas why? thanks
<html> <head> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script> function radioclicked(){ if ($('#radio1').attr('checked') == 'checked') alert('radio 1 selected'); else if ($('#radio2').attr('checked') == 'checked') alert('radio 2 selected'); else if ($('#radio3').attr('checked') == 'checked') alert('radio 3 selected'); else alert('none selected'); } </script> </head> <body> <label for="radio1">radio 1</label><input type="radio" id="radio1" name="radios" onclick="radioclicked()" /> <label for="radio2">radio 2</label><input type="radio" id="radio2" name="radios" onclick="radioclicked()" /> <label for="radio3">radio 3</label><input type="radio" id="radio3" name="radios" onclick="radioclicked()" /> </body> </html>
to see if checkbox checked, should using .is(':checked')
function radioclicked(){ if ($('#radio1').is(':checked')) alert('radio 1 selected'); else if ($('#radio2').is(':checked')) alert('radio 2 selected'); else if ($('#radio3').is(':checked')) alert('radio 3 selected'); else alert('none selected'); }
an easier way check checkboxes starting "radio", be:
function radioclicked(){ $('[id^="radio"]').filter(':checked').each(function(i,ele) { alert(ele.id + ' checked'); }) }
and since have same name , belong same group, 1 radio can checked, do:
function radioclicked(){ alert( $('[name=radios]:checked').prop('id') + ' checked'); }
Comments
Post a Comment