jQuery jquery中选定单选按钮的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6913848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:33:00  来源:igfitidea点击:

count of selected radio buttons in jquery

jqueryjquery-selectors

提问by LocustHorde

suppose I have groups of radio buttons like so:

假设我有一组单选按钮,如下所示:

<label><input type="radio" value="1" name="rdQueen" /> Scaramouche</label> <br />
<label><input type="radio" value="1" name="rdQueen" /> Will you do the</label> <br />
<label><input type="radio" value="1" name="rdQueen" /> Fandango</label> <br />

... sometime later in the page ...

......稍后在页面中......

<label><input type="radio" value="1" name="rdFruit" /> Mango</label> <br />
<label><input type="radio" value="1" name="rdFruit" /> Kiwi</label> <br />
<label><input type="radio" value="1" name="rdFruit" /> Potato</label> <br />

All I want to do is make sure atleast one of them from both group has been selected.. so I need to count the radiobuttons that have been checked, in this case it'll be 2.

我想要做的就是确保至少从两个组中选择了一个..所以我需要计算已检查的单选按钮,在这种情况下它将是 2。

Only, I am not sure how to do that. help please!

只是,我不知道该怎么做。请帮忙!

回答by AlienWebguy

To check for only those specific groups:

仅检查那些特定组:

$(':radio[name="rdQueen"]:checked, :radio[name="rdFruit"]:checked').length;

Example: http://jsfiddle.net/AlienWebguy/HzfKq/

示例:http: //jsfiddle.net/AlienWebguy/HzfKq/

回答by Nicola Peluchetti

You could do:

你可以这样做:

 var numberOfCheckedRadio = $('input:radio:checked').length
 //this gives you the total of checked radio buttons on the page

回答by user113716

Use the checked-selector[docs]to get the ones that are checked, and the length[docs]property to find out how many there were.

使用checked-selector[docs]获取已检查的内容,并使用length[docs]属性找出有多少。

alert( $('input:radio:checked').length );