jquery中所选单选按钮的数量

Loc*_*rde 9 jquery jquery-selectors

假设我有这样的单选按钮组:

<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 />
Run Code Online (Sandbox Code Playgroud)

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

<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 />
Run Code Online (Sandbox Code Playgroud)

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

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

Ali*_*guy 16

要仅检查那些特定组:

$(':radio[name="rdQueen"]:checked, :radio[name="rdFruit"]:checked').length;
Run Code Online (Sandbox Code Playgroud)

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


Nic*_*tti 9

你可以这样做:

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


use*_*716 6

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

alert( $('input:radio:checked').length );
Run Code Online (Sandbox Code Playgroud)