How to check a radio button with jQuery?

前端 未结 30 2133
独厮守ぢ
独厮守ぢ 2020-11-22 08:12

I try to check a radio button with jQuery. Here\'s my code:

30条回答
  •  攒了一身酷
    2020-11-22 09:10

    The $.prop way is better:

    $(document).ready(function () {                            
        $("#radio_1").prop('checked', true);        
    });
    

    and you can test it like the following:

    $(document).ready(function () {                            
        $("#radio_1, #radio_2", "#radio_3").change(function () {
            if ($("#radio_1").is(":checked")) {
                $('#div1').show();
            }
            else if ($("#radio_2").is(":checked")) {
                $('#div2').show();
            }
            else 
                $('#div3').show();
        });        
    });
    

提交回复
热议问题