how to set radio option checked onload with jQuery

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

    If you want it to be truly dynamic and select the radio that corresponds to the incoming data, this works. It's using the gender value of the data passed in or uses default.

    if(data['gender'] == ''){
     $('input:radio[name="gender"][value="Male"]').prop('checked', true);
    }else{
      $('input:radio[name="gender"][value="' + data['gender'] +'"]').prop('checked', true);
    };
    

提交回复
热议问题