jQuery Radio button
I was wondering why this isn't working properly on IE, when you choose a radio button?
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("input[name='crkbrd']").change(function(){
if ($("input[name='crkbrd']:checked").val() == 'Upload')
{ alert("Upload Your Own Ad"); }
else if ($("input[name='crkbrd']:checked").val() == 'Edit')
{ alert("Edit Your Ad Here"); }
else
{ alert("You haven't chosen anything"); }
});
});
</script>
</head>
<body>
<input type="radio" id="rb1" name="crkbrd" value="Upload" /> Upload Your Own Ad<br />
<input type="radio" id="rb2" nam开发者_StackOverflow社区e="crkbrd" value="Edit" /> Edit Your Ad Here
</body>
</html>
IE has a bug with radio buttons in that it doesn't fire the change event until the radio button is blurred. Use the click event instead.
Bug details: http://webbugtrack.blogspot.com/2007/11/bug-193-onchange-does-not-fire-properly.html
Note this bug was fixed in IE9 (as long as you are running in standards mode)
I have this working on IE 8, Firefox and chrome browsers
Try assigning a class
attribute to the radio buttons as:
<input type="radio" id="rb1" name="crkbrd" value="Upload" class="myRadio" /> Upload Your Own Ad<br />
<input type="radio" id="rb2" name="crkbrd" value="Edit" class="myRadio" /> Edit Your Ad Here
and then use the following:
$( ".myRadio" ).change( function(){
if ($(this).val() == 'Upload') {
alert("Upload Your Own Ad");
} else
if ($(this).val() == 'Edit') {
alert("Edit Your Ad Here");
} else {
alert("You haven't chosen anything");
}
});
精彩评论