how to set radio option checked onload with jQuery

后端 未结 16 2095
陌清茗
陌清茗 2020-12-04 04:42

How to set radio option checked onload with jQuery?

Need to check if no default is set and then set a default

16条回答
  •  一生所求
    2020-12-04 05:21

    Say you had radio buttons like these, for example:

    
    
    

    And you wanted to check the one with a value of "Male" onload if no radio is checked:

    $(function() {
        var $radios = $('input:radio[name=gender]');
        if($radios.is(':checked') === false) {
            $radios.filter('[value=Male]').prop('checked', true);
        }
    });
    

提交回复
热议问题