views:

371

answers:

1

I'm trying to have an alert box show the value of the radio option I have selected. HTML:

<input type="radio" name="sex" value="Male" /> Male<br />
<input type="radio" name="sex" value="Female" /> Female

When I choose one of those and set up and alert box to show me what i choose (as a test), it doesn't work. I know that other people are telling to set this up like:

var sex = $('input[name=sex]:checked').val();

However, when i put that, the entire script just craps on itself. Nothing works. The script I have before looked like:

var sex = $('input[name=sex]:checked');

and with that the script still worked, but the alert only showed "[object Object]". What am i doing wrong?

A: 

Usually the value of a radio button is on/off. You might want to try using:

 var sex = $('input[name=sex]:checked').attr('value');

On the other hand, I don't see anything about your code that should cause it to fail (unless there's a bug in jQuery that can't handle your custom values).

Have you used Firefox/Firebug to see what the console error is? It may be something completely unrelated.

tvanfosson
I dont know how to use FireBug to find errors, only temporarily edit script.
Your code made it stop too. I have a form with radio's and when i hit submit it went to the action, the jQuery didn't disable it's default.
You need to post more code. Also, if you want to prevent a form from submitting in a jQuery callback you need to prevent the default action , either explicitly or by returning false from the callback.
tvanfosson