Checking if radio buttons are checked in Firefox

Posted by Andrew Song on Stack Overflow See other posts from Stack Overflow or by Andrew Song
Published on 2009-10-09T15:24:34Z Indexed on 2010/06/16 6:52 UTC
Read the original article Hit count: 265

Filed under:
|
|
|

On my site, I have two checkboxes created in my ASP.NET MVC view like so:

Html.RadioButton("check", "true", "true" == (string) ViewData["someKey"], new { id = "check1"});
Html.RadioButton("check", "false", "false" == (string) ViewData["someKey"], new { id = "check2"});

I am positive that ViewData["someKey"] has the value "true" in it.

In my JS init function, I perform the following check:

alert($('#check1').is(':checked') + " " + $('#check2').is(':checked'));

In Firefox (and only Firefox), my alert dialog will show the following (it works as expected in every other browser):

Initial page load: true false
Normal refresh via Ctrl + R: false false
Refresh skipping cache via Ctrl + Shift + R: true false

I have tried many different methods of looking at the checkbox value, including $('#check1').attr('checked') without success. If I examine the HTML in Firebug, I can see that the first radio button has the property checked="checked" in it.

Why is the checkbox value changing in FF when I refresh, and how can I mitigate this? Since this seems to be a FF-only bug, how can I change my code to make it work?

This SO question seemed to ask something similar, but none of the proposed solutions seem to work in this case.

Edit: I should also point out that when the radio button is rendered after the refresh in FF, it's not actually being displayed as checked either, despite what the HTML is telling me.

Edit2: Adding raw HTML as per request

<input id="check1" type="radio" value="True" name="check" checked="checked"/>
<input id="check2" type="radio" value="False" name="check"/>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery