2013-08-05 41 views
0

我有一個測驗通過以下方式設置:使用jQuery檢查,如果一個在組單選按鈕被選中

與右邊左邊的問題,真或假單選按鈕的表。

在測驗的底部,我有一個第2步按鈕,點擊後,拉動Fancybox圖層以在表單中詢問該人的聯繫信息。

我想停止fancybox窗口打開,除非爲每個問題選擇了一個答案(true或false)。如果用戶沒有選擇答案,我想顯示一條消息,讓他們知道必須在未答覆的問題下面選擇答案。

我已經使用這個代碼的onClick嘗試:

$("input:radio:not(:checked)").parent().parent().append("ERROR") 

唯一的問題是,這種代碼是不承認他們是對的單選按鈕,而不僅僅是個人的,而且它應該只標註錯誤如果他們都沒有被選中。

下面是我的標記示例。

<tr class="alternate"> 
     <td>At school, you have to reminded to do your assignments.</td> 
     <td class="centered"><input type="radio" class="radio" name="Question_1" value="True" /></td> 
     <td class="centered"><input type="radio" class="radio" name="Question_1" value="False" /></td> 
    </tr> 

任何關於如何改進我的jQuery來完成這一任務的建議?

回答

3

嘗試

$('tr').not(':has(:radio:checked)').addClass("ERROR"); 

演示:Fiddle

+0

@mishik你可以只選擇一個同名的單選按鈕 –

2

例如,檢查每個tr有且只有一個input檢查:

$("tr").each(function(i, el) { 
    if($(this).find("input:checked").length != 1) { 
    $(this).addClass("ERROR"); 
    } 
}); 
0

我有一個小的邏輯你,但它是隻是一個想法,

$(function() { 
    var flag=true; 
    $("tr").each(function(i, el) { 
     if($('[name=Question_'+i+']:checked').length == 0){ 
      //show the error message here for that particular div 
      //Set the flag= flase; 
      flag= false;   
     }else{ 
      flag= true; 
     } 
    }); 
    if(flag){ 
    //Success, all the questions are answered  
    }else{ 
    //Show the error Message.  
    } 

    }); 

當複選框被選中時,您將有標誌狀態爲真。

@ArunPJohny,老兄,我想獲得你的建議。

相關問題