开发者

count of selected radio buttons in jquery

开发者 https://www.devze.com 2023-03-24 21:59 出处:网络
suppose I have groups of radio buttons like so: <label><input type=\"radio\" value=\"1\" name=\"rdQueen\" /> Scaramouche</label> <br />

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="开发者_StackOverflowrdQueen" /> 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.

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


To check for only those specific groups:

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

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


You could do:

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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消