how to set radio option checked onload with jQuery

后端 未结 16 2102
陌清茗
陌清茗 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:32

    I think you can assume, that name is unique and all radio in group has the same name. Then you can use jQuery support like that:

    $("[name=gender]").val(["Male"]);
    

    Note: Passing array is important.

    Conditioned version:

    if (!$("[name=gender]:checked").length) {
        $("[name=gender]").val(["Male"]);
    }
    

提交回复
热议问题