如何使用jQuery设置单选选项检查onload [英] how to set radio option checked onload with jQuery

查看:60
本文介绍了如何使用jQuery设置单选选项检查onload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery设置单选选项检查onload?

How to set radio option checked onload with jQuery?

需要检查是否未设置默认值,然后设置默认值

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

推荐答案

假设您有这样的单选按钮,例如:

Say you had radio buttons like these, for example:

<input type='radio' name='gender' value='Male'>
<input type='radio' name='gender' value='Female'>

如果您未选中任何广播,那么您想检查一个值为"Male"的负载:

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);
    }
});

这篇关于如何使用jQuery设置单选选项检查onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆