Kurdt94
Kurdt94

Reputation: 95

I get the wrong radiobutton value returned , why?

This returns the value of the previous selected radio button value, why ?

<script>

    $("#ptmodel label").click(function () {

 var selected = $("input[name=in_pt_model]:checked").val(); 
 var selecteddataString = 'in_pt_model='+ selected;
 $.ajax({
    type: "POST",
    url: "badkamer_sets.php",
    data: selecteddataString,
    success: function(selected){
            $("#ptsize_check").html(selected);
    }
});
});
</script>

Upvotes: 1

Views: 1219

Answers (2)

Elia Weiss
Elia Weiss

Reputation: 9896

My gut tells me that the underlying issue here is that the jquery onclick event you are attaching to the radio button is firing before the DOM handles the click event, meaning the radio button has not actually been clicked yet, so when you check the .val() it still reports the old value. Changing the event from click to change should fix this. –

Upvotes: 0

Chandu
Chandu

Reputation: 82903

I guess #ptmodel label is the label associated with the input[name=in_pt_model].

Instead of using the click event of the label (assosicated with input[name=in_pt_model]").) handle the change event of input[name=in_pt_model]").

Try this:

<script>
    $("#ptmodel input[name=in_pt_model]").change(function () {
     var selected = $(this).val(); 
     var selecteddataString = 'in_pt_model='+ selected;
     $.ajax({
            type: "POST",
            url: "badkamer_sets.php",
            data: selecteddataString,
            success: function(selected){
                                    $("#ptsize_check").html(selected);
            }
        });
    });
</script>

Upvotes: 4

Related Questions