2013-02-01 37 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個收音機,則所有其他收音機都將被禁用。