Javascript/Jquery cannot use 0 or 1 as the value of the variable?
I have a开发者_StackOverflow form with a radio select box. There are 6 options. The values of these options are 0 through 5. I'm using Jquery to retrieve the value of the checked radio button upon submission of the form.
var nextStatus;
nextStatus = $j('input[name="Status"]:radio:checked').val();
This works fine when the status is 2 or above, however, when the status is 0 or 1, it does not set the variable at all. I cannot figure out why it would matter that the value of the radio select button being 0 or 1 would cause jquery/javascript to not set this variable.
<label><input type='radio' name='Status' value='0' id='0' />Status0</label><br />
<label><input type='radio' name='Status' value='1' id='1' />Status1</label><br />
<label><input type='radio' name='Status' value='2' id='2' />Status2</label><br />
<label><input type='radio' name='Status' value='3' id='3' />Status3</label><br />
<label><input type='radio' name='Status' value='4' id='4' />Status4</label><br />
<label><input type='radio' name='Status' value='5' id='5' />Status5</label><br />
When I use parseInt() on this variable, any status 2 or above comes back fine, but if its the 0 or 1, it returns NaN. When I use typeof() on the var, it returns "string" (after removing parseInt()). I'm totally stumped on this. I SHOULD be able to use a 0 or a 1 as a variable value, should I not?
If I change the values of the radio button to 2 through 7 instead 0 through 5, using firebug, it works fine. I'd rather not do that as this radio select is programatically generated from a database using foreign keys, and it would be difficult to modify all of that for an issue that to my knowledge shouldn't even be happening.
If anyone has run into this, please help!!!
It's perhaps worth pointing out that, under HTML 4, your input elements' id s are invalid: they shouldn't start with a numeric character. – David Thomas 14 mins ago
I appened a string to the beginning of the IDs, and this fixed the issue!
Works for me: http://jsfiddle.net/JYC3F/1/
Please create a working pared-down test case showing your problem (preferably at JSFiddle).
精彩评论