开发者

I get the wrong radiobutton value returned , why?

开发者 https://www.devze.com 2023-03-20 10:54 出处:网络
This returns the value of the previous selected radio button value, why ? <script> $(\"#ptmodel label\").click(function () {

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_mode开发者_运维知识库l='+ selected;
 $.ajax({
    type: "POST",
    url: "badkamer_sets.php",
    data: selecteddataString,
    success: function(selected){
            $("#ptsize_check").html(selected);
    }
});
});
</script>


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>


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. –

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号