2013-02-01 36 views
-3

我想验证在form.kindly帮助中的五个单选按钮组中选择了至少三个单选按钮。如何验证从5个单选按钮组中选择了至少3个单选按钮

您的出席

<tr> 
    <th > Your grades <font size="4" > </font></th> 
    <td> <input type="radio" name ="v2" value = "4" onclick="updateTotal();" /></td> 
    <td> <input type="radio" name ="v2" value = "3" onclick="updateTotal();" /></td> 
    <td> <input type="radio" name ="v2" value = "2" onclick="updateTotal();" /></td> 
    <td> <input type="radio" name ="v2" value = "1" onclick="updateTotal();" /></td>  
    </tr> 

    <tr> 
    <th >Your self-control <font size="4" > </font></th> 
    <td> <input type="radio" name ="v3" value = "4" onclick="updateTotal();" /></td> 
    <td> <input type="radio" name ="v3" value = "3" onclick="updateTotal();" /></td> 
    <td> <input type="radio" name ="v3" value = "2" onclick="updateTotal();" /></td> 
    <td> <input type="radio" name ="v3" value = "1" onclick="updateTotal();" /></td>  
    </tr> 

你的自我
控制

FD

+0

在这种情况下,“您的出席”,“您的自我”,“控制”和“fd”是什么意思? – Jules

回答

0
var minThreeChecked = $('input[type="radio"]').filter(':checked').length >= 3; 

可以结合使用这些选择,但这种方式效率更高。

0

您可以使用

$('input[type=radio]:checked').length >= 3 

如果你需要更确切的一点关于组,使用

$('input[name^=v][type=radio]:checked').length >= 3 
1
if ($('input[type=radio]:checked').length >= 3) { 
    //what to do if 3 or more are selected 
} 

基本上这样做的以下内容::checked选择查找任何被检查的无线电输入(您可能只需要选择特定div中的输入等,以避免拉入页面上的其他单选按钮)。默认情况下,jQuery在选择器中构建匹配元素的集合,然后返回。由于这只是一个标准数组,因此可以通过获取数组的length属性来计算匹配了多少个元素。如果有3个以上检查单选按钮,阵列将拥有至少3的长度,和你的if声明将评估为true

+0

你可以通过解释它来改进这个答案(不要只提供一条鱼,教导OP如何更好地钓鱼)。 –

+0

@EricJ。添加了对此答案的解释。 – brandwaffle

0

我认为你正在寻找这样的:http://jsfiddle.net/2dfRd/

$(':radio').change(function (e) { 
    var rad = $('[type="radio"]:checked').length; 
    if (rad >= 3) { 
    $(':radio').not(':checked').prop('disabled', true); 
    alert('3 radios selected.'); 
    } 
}); 

这里如果您选中了3个收音机,则所有其他收音机都将被禁用。