3

I'm using jQuery .val() to pull data results out of a form that contains two sets of radio buttons.

<!DOCTYPE HTML>
<html>
    <head>  

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

    <script type="text/javascript">

    function userPrefsSubmitted() {
    console.log(  $('form input[@name=cssOn]:checked').val()   );
    console.log(  $('form input[@name=halfFramesOn]:checked').val()   );
    }   
    </script>

</head>
<body>  
<p>Choose User Prefs</p>
<form>
    <p>Transitions</p>
    <div><input type="radio" name="cssOn" value=true>True</div>
    <div><input type="radio" name="cssOn" value=false>False</div>
    <p>Half Frames</p>  
    <div><input type="radio" name="halfFramesOn" value=true>True</div>
    <div><input type="radio" name="halfFramesOn" value=false>False</div>
    <br/><br/>
    <input class="submitButton" type="button" onClick="userPrefsSubmitted()" value="tap when done" />
</form> 

</body>
</html>

If I choose fasle then true and hit the button the console.log shows false false

This is driving me nuts and I've search this forum as well as interwebs but not found an answer. Where am I going wrong?

1
  • @Andrew: put it as an answer ;-) Commented Aug 1, 2012 at 3:45

4 Answers 4

9

Without @. you can do it easily:

function userPrefsSubmitted() {
  console.log(  $('form input[name=cssOn]:checked').val()   );
  console.log(  $('form input[name=halfFramesOn]:checked').val()   );
}   
Sign up to request clarification or add additional context in comments.

Comments

2

OR try this: working demo http://jsfiddle.net/4T5r3/

Note: http://www.electrictoolbox.com/at-name-selector-removed-jquery/

The use of these selectors without the @ also works in the jQuery 1.2 branch. I am not sure if it works in older versions.

code

function userPrefsSubmitted() {
    alert($('form input[@name=cssOn]:checked').prop('value'));
    alert($('form input[@name=halfFramesOn]:checked').prop('value'));
}​

J 1.2 >

  function userPrefsSubmitted() {
        alert($('form input[name=cssOn]:checked').prop('value'));
        alert($('form input[name=halfFramesOn]:checked').prop('value'));
    }​

working image When true selecte

enter image description here working image When false selected

enter image description here

10 Comments

@thecodeparadox thanks bruv! you back in rocking mode again :P
This doesn't work...same problem as in the question. The '@' makes the attribute-equals-selector fail, so it will always alert the value of the first radio button. In your demo try choosing 'true', 'false' -- it still alerts 'true', 'true'.
@nbrooks are you sure man try this: jsfiddle.net/4T5r3 select the radio button and then click the button, working finr here lion osx and tried on windows as well {chrome - browser} i.e. when selected true it returns true and when selected false it alerts false
The values don't have to be the same though. If you select 'true' in the first radio group, and 'false' in the second radio group, your alerts should show "true" then "false". This shows "true" then "true". It's easy to see the error if you make the values different jsfiddle.net/4T5r3/1 (it alerts the first value twice)
@nbrooks aaa got you phew :P The use of these selectors without the @ also works in the jQuery 1.2 branch. I am not sure if it works in older versions. electrictoolbox.com/at-name-selector-removed-jquery thanks man
|
1
function userPrefsSubmitted() {
console.log(  $('form input[name=cssOn]:checked').val()   );
console.log(  $('form input[name=halfFramesOn]:checked').val()   );
}   

Comments

0

Also please try $(":checked").val()

instead of

$('form input[@name=halfFramesOn]:checked').val()

This will simply return all checked radio buttons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.