how to set radio option checked onload with jQuery

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

    I liked the answer by @Amc. I found the expression could be condensed further to not use a filter() call (@chaiko apparently also noticed this). Also, prop() is the way to go vs attr() for jQuery v1.6+, see the jQuery documentation for prop() for the official best practices on the subject.

    Consider the same input tags from @Paolo Bergantino's answer.

    
    
    

    The updated one-liner might read something like:

    $('input:radio[name="gender"][value="Male"]').prop('checked', true);
    

提交回复
热议问题