2012-08-30 7 views
-2

나는 하나 개의 라디오 버튼을 체크 모든 문제의 경우 자바 스크립트 쉽고 간단한 방법으로 확인하는 방법이 코드자바 스크립트 확인 라디오 버튼

<?php foreach($this->question as $question): ?> 
<div class="question"> 
    <?php echo $question['question'] ?>   </div> 


<?php if($this->activeEdition["id"]!=20) { ?> 

<div class="answers"> 
    <?php 
    $i = 1; 
    foreach($question['answers'] as $answer): 
    ?> 
    <input type="radio" name="question[<?php echo $question['id'] ?>]" value="<?php echo $answer['id'] ?>" id="<?php echo 
      $answer['id'] 
    ?>" class="radio_answer radio_answer_<?php echo $i; ?>" > 
    <label for="<?php echo $answer['id'] ?>"><?php echo $answer['answer'] ?></label> 
    <?php 
    if(count($question['answers']) > 3){ 
    echo '<br/>'; 
    } 
    ?> 
<?php 
$i++; 
endforeach; 
?> 
</div> 

있어?

답변

0

모든 대답이 필요한 경우 생성 할 때 첫 번째 라디오를 모든 대답에서 선택하도록 설정해야합니다. 그것은 가장 쉬운 방법입니다.

물론 JS로 확인할 수는 있지만 추한 것입니다. 당신은 당신이 그것을 간단하게 할 수 어쩌면와 in_array 확인하려면 몇 가지 더 나은 솔루션이 질문의 수를 알고있는 경우

<script> 
    function check(){ 
     var inputs = document.getElementsByTagName('input'); 
     var names = []; 
     var checked = 0; 

     for(var i=0;i<inputs.length;i++){ 
      if(inputs[i].type=='radio'){ 
       inArray = false; 
       for(var j=0;j<names.length;j++){ 
        if(names[j]==inputs[i].name){ 
         inArray = true; 
         break; 
        } 
       } 
       if(!inArray){ 
        names.push(inputs[i].name); 
       } 
       if(inputs[i].checked){ 
        checked++; 
       } 
      } 
     } 

     return names.length==checked; 
    } 
</script> 
<input type="radio" name="a"> 
<input type="radio" name="a"><br/> 
<input type="radio" name="b"> 
<input type="radio" name="b"><br/> 
<input type="radio" name="b"> 
<input type="radio" name="c"> 
<input type="radio" name="c"><br/> 
<input type="radio" name="d"> 
<input type="radio" name="d"> 
<input type="radio" name="d"> 
<input type="button" onClick="alert(check());"> 

: 여기 데모 코드입니다.

jQuery가 더 좋습니다.

0

나는

<script type="text/javascript"> 
function checkform() 
{ 
    var ilosc = document.getElementsByClassName('question'); 
    var n = $("input:checked").length; 
    if (n != ilosc.length) 
    { 
    alert('Proszę zaznaczyć wszyskie odpowiedzi'); 

    return false; 
    }else return true; 

} 
</script> 
jQuery를

를 사용하여 더 나은 솔루션을 완료 확인