如何使用JQuery检查单选按钮? [英] How to check radio button is checked using JQuery?

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

问题描述

我在一个组中有两个单选按钮,我想检查单选按钮是否已使用JQuery检查,如何?

I have two radio buttons in one group, I want to check the radio button is checked or not using JQuery, How ?

推荐答案

给定一组单选按钮:

<input type="radio" id="radio1" name="radioGroup" value="1">
<input type="radio" id="radio2" name="radioGroup" value="2">

您可以测试是否使用jQuery检查特定的一个,如下所示:

You can test whether a specific one is checked using jQuery as follows:

if ($("#radio1").prop("checked")) {
   // do something
}

// OR
if ($("#radio1").is(":checked")) {
   // do something
}

// OR if you don't have ids set you can go by group name and value
// (basically you need a selector that lets you specify the particular input)
if ($("input[name='radioGroup'][value='1']").prop("checked"))

您可以按如下方式获取组中当前已检查的值:

You can get the value of the currently checked one in the group as follows:

$("input[name='radioGroup']:checked").val()

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

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