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

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

问题描述

如何使用 jQuery 设置选中的单选选项?

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" onload 的那个:

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 设置选中的单选选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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