2011-07-27 2 views
0

나는 한 쌍의 질문으로 설문 조사를합니다. 여기에 HTML 코드가 있습니다jquery validation

<form id="pool"> 
<div class="questions> 
    <input type="radio" name="sex">Male 
    <input type="radio" name="sex">Female 
</div> 

<div class="questions> 
    <input type="radio" name="hair">Brown 
    <input type="radio" name="hair">Blonde 
</div> 

.... a lot of qestions div's 
</form> 

모든 div`s에 체크 된 라디오 버튼이 있는지 확인하기 위해 양식을 제출 한 후 어떻게해야합니까?

+0

가능한 중복 매우 효율적입니다 내 경험에서 jquery validate plugin

을 사용할 수 있습니다 [결정하면 모든 라디오 그룹 옵션을 선택했다] (http://stackoverflow.com/questions/6796870/determine-if-every-radio-group-has-had-an-option-selected) – Paulpro

답변

1

do :

if($('#pool input:radio:checked').length < numGroups){ 
    // At least one group isn't checked 
} 

그렇지 않으면 그룹 수를 먼저 계산해야합니다. 나는이 더 나은 다음을 수행 할 수있는 방법을 생각할 수 없습니다

var rgroups = []; 
$('#pool input:radio').each(function(index, el){ 
     var i; 
     for(i = 0; i < rgroups.length; i++) 
      if(rgroups[i] == $(el).attr('name')) 
       return true; 
     rgroups.push($(el).attr('name')); 
    } 
); 
rgroups = rgroups.length; 

if($('#pool input:radio:checked').length < rgroups) 
    alert('You must fill in all the fields.'); 
else 
    alert('Thanks!'); 
0

기본값을 설정하거나 제출 단추에 대한 처리기를 만들고 일부 값이 점검되었는지 확인하십시오. 더 라디오 버튼이 선택되지 않은 경우, 오류 메시지가 표시 및 형태 (false를 반환)

0

테스트되지 않은 코드를 제출하지 않는, 그러나 이것은 생각이다 : 당신이 알고 있다면 당신은 그냥 할 수있는 얼마나 많은 그룹

if($('#pool').children().length == $('pool 
div').find('#pool input:radio:selected').length) { 
//do stuff 
}