Tim Zhukov-Khovanskiy
Tim Zhukov-Khovanskiy

Reputation: 500

Multiple radio button groups interfering with each other

I have two groups of radio buttons set up in one form:

<li>1&nbsp;<Input type = 'radio' id ='part1no' Name ='part1' value= '0'>No
           <Input type = 'radio' id ='part1yes' Name ='part1'  checked="checked" value= '1'>Yes
</li>
<li>2&nbsp;<Input type = 'radio' id ='part2no' Name ='part2'  value= '0'>No
           <Input type = 'radio' id ='part2yes' Name ='part2' checked="checked" value= '1'>Yes
</li>

I'm trying to submit the values of those buttons via this javascript function:

var dataString = 'username='+SessionVars.my_username+'&lessonid='+SessionVars.my_lesson_number;

$('#tracking_submit').click(function(){
    $.ajax({
        url: "php/tracking.php",
        type:'POST',
        data: dataString+'&p1='+$("input[@name=part1]:checked").val()+'&p2='+$("input[@name=part2]:checked").val(),
        success: function(){
            $('#tracking_message').html("Thank you for updating.").fadeIn('slow');              
            setTimeout(function(){$('#tracking_message').fadeOut('slow');},2000);
        }                   
    });
    return false;   
});

Issue is that when I check the database, it updates both fields set to the value of the first radio button. If the first ones is set to Yes, both will be set to 1 and if the first one is set to 0, both would be 0. Anyone has any ideas as to what may be causing this?

Upvotes: 1

Views: 663

Answers (1)

jimp
jimp

Reputation: 17487

The $("input[@name=part1]:checked").val() should not have the @ character. That is XPath syntax, not jQuery selector syntax.

jQuery is probably ignoring that "@attribute" request (since it's invalid), finding all input's, and then .val() is returning the first value in the set.

Upvotes: 1

Related Questions