2017-12-12 17 views
1

現在、各タブに1つの質問を表示するスキルテストプロジェクトで作業しています。回答の選択肢はラジオボタンです。ラジオボタンが選択されたとき(ユーザーが質問に回答したとき)にタブの色を変更して、質問にすでに回答したことを示すようにしたい。私は誰かが私にこの新しいことを教えてくれることを願っています..ありがとうございました!ここに私のコードです:ラジオボタンが既に選択されているときのタブの色の変更

(私は、関連する彼らを見て、私の質問を短くするためにいけないので、私は私のコードの一部が含まれてdidntの)

スタイル:

div.tab { 
    overflow: hidden; 
    border: 1px solid #ccc; 
    background-color: #f1f1f1; 
} 

div.tab button { 
    background-color: inherit; 
    float: left; 
    border: 1px solid #ccc; 
    outline: none; 
    cursor: pointer; 
    padding: 13px 12.62px; 
    transition: 0.3s; 
    font-size: 10px; 
} 

div.tab button:hover { 
    background-color: #ddd; 
} 

div.tab button.active { 
    background-color: #d6f5d6; 
} 

.tabcontent { 
    display: none; 
    border: 1px solid #ccc; 
    border-top: none; 
} 

本体:

<body onload="document.getElementById('defaultOpen').click();"> 
<script> 
    $(document).ready(function(){ 
    $(".nav-tabs a").click(function(){ 
     $(this).tab('show'); 
    }); 
    }); 
</script> 

<div class="col-md-auto col-md-offset-1 col-centered"> 
    <div class="panel panel-success"> 
     <script> 
     function openTab(evt, tabName) { 
      var i, tabcontent, tablinks; 
      tabcontent = document.getElementsByClassName("tabcontent"); 
      for (i = 0; i < tabcontent.length; i++) { 
       tabcontent[i].style.display = "none"; 
      } 
      tablinks = document.getElementsByClassName("tablinks"); 
      for (i = 0; i < tablinks.length; i++) { 
       tablinks[i].className = tablinks[i].className.replace(" active", ""); 
      } 
      document.getElementById(tabName).style.display = "block"; 
      evt.currentTarget.className += " active"; 
     } 
     document.getElementById("defaultOpen").click(); 

     </script> 

     <div class="tab"> 
      <button class="tablinks active" type="button" onclick="openTab(event, 'q1')" id="defaultOpen"></button> 
      <button class="tablinks" type="button" onclick="openTab(event, 'q2')"></button> 
      <button class="tablinks" type="button" onclick="openTab(event, 'q3')"></button> 
      <button class="tablinks" type="button" onclick="openTab(event, 'q4')"></button> 
      <button class="tablinks" type="button" onclick="openTab(event, 'q5')"></button> 
     </div>    

     <?php if (mysqli_num_rows($result) > 0): ?> 
     <?php $index = 0; $num=0; ?> 
       <div id="q<?php echo ($index++); ?>" class="tabcontent"> 
        <table class="table table-hover"> 
        <tbody> 
         <tr class="form-group"> 
          <h3 name="ques[<?php echo $test_id;?>]" style="text-indent: 40px;"> <?php echo $num,'. ', $question; ?> </h3> 
         </tr> 
         <tr class="form-group"> 
          <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $test_id;?>]" value="<?php echo $optiona;?>"><?php echo $optiona;?> 
          </label> 
          <br> 
          <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $test_id;?>]" value="<?php echo $optionb;?>"><?php echo $optionb; ?> 
          </label> 
          <br> 
          <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $test_id;?>]" value="<?php echo $optionc;?>"><?php echo $optionc;?> 
          </label> 
          <br> 
          <label class="radio-inline" style="text-indent: 70px; font-size: 18px;"> 
           &nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $test_id;?>]" value="<?php echo $optiond;?>"><?php echo $optiond;?> 
          </label> 
          <br> 
         </tr> 
        </tbody> 
        </table> 
        <center> 
         <button class="btn btn-default btnBack" data-direction="back" type="button" onclick="openTab(event, 'q<?php echo ($index-=1); ?>')"><span class="glyphicon glyphicon-triangle-left"></span> Back</button> 
         <button class="btn btn-default btnNext" data-direction="next" type="button" onclick="openTab(event, 'q<?php echo ($index+=1); ?>')"><span class="glyphicon glyphicon-triangle-right"></span> Next</button> 
        </center> 
        <br> 
       </div> 
      <?php $num++; ?> 
     <?php endif ?> 
    </div> 
    </div> 
</div> 
</body> 

答えて

0

私はあなたのコードのJSbinを作った:

https://jsbin.com/conuhow/edit?js,console,output 

PHPパーツがないため、いくつか変更があります。

希望すると便利です。楽しい時間をお過ごしください!

コードベロー:

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Questions</title> 
<script language="javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
</head> 

    <body onload="document.getElementById('defaultOpen').click();"> 

    <style> 

     div.tab { 
      overflow: hidden; 
      border: 1px solid #ccc; 
      background-color: #f1f1f1; 
     } 

     div.tab button { 
      background-color: inherit; 
      float: left; 
      border: 1px solid #ccc; 
      outline: none; 
      cursor: pointer; 
      padding: 13px 12.62px; 
      transition: 0.3s; 
      font-size: 10px; 
     } 

     div.tab button:hover { 
      background-color: #ddd; 
     } 

     div.tab button.active { 
      background-color: #d6f5d6 !important; 
     } 

     .input-style { 
      font-size: 18px; 
      text-indent: 70px; 
     } 

     .tabcontent { 
      display: none; 
      border: 1px solid #ccc; 
      border-top: none; 
     } 

     .btnSend { 
      background-color: rgba(94,191,79,1.00); 
      color: #fff; 
      border: 0px; 
      padding: 15px; 
      border-radius: 7px; 
     } 

     .tabchanged { 
      background-color: rgba(41,215,217,1.00) !important; 
     } 


    </style> 

    <script> 

     var questionSelected = 1; 
     var questionsReply = { 
      "question1" : 0, 
      "question2" : 0, 
      "question3" : 0, 
      "question4" : 0, 
      "question5" : 0 
     }; 

     $(document).ready(function(){ 
      $(".nav-tabs a").click(function(){ 
       $(this).tab('show'); 
      }); 

      $(".answer").click(function(){ 
       var clickedquestion = $(this).attr("question"); 
       var clickedanswer = $(this).attr("answer"); 
       if (clickedquestion == 1) { questionsReply.question1 = clickedanswer; } 
       else if (clickedquestion == 2) { questionsReply.question2 = clickedanswer } 
       else if (clickedquestion == 3) { questionsReply.question3 = clickedanswer } 
       else if (clickedquestion == 4) { questionsReply.question4 = clickedanswer } 
       else if (clickedquestion == 5) { questionsReply.question5 = clickedanswer } 
      });   
     }); 

     function sendResult() { 
      alert("Object of Answers (PARSE JSON TO PHP BACK-END):", questionsReply); 
     } 

     function openTab(evt, tabName) { 

      questionSelected = tabName; 
      console.log("Tab: " + questionSelected); 
      console.log("Array of Answers :", questionsReply); 

      $(".tabcontent").hide(); 
      $(".tablinks").removeClass('active'); 
      $("#q"+tabName).show(); 

      //COLORTABS before .active background; 
      colorTabs(); 
      $(".tablinks[tabid="+questionSelected+"]").addClass("active"); 

      evt.currentTarget.className += " active"; 

      if (questionSelected = 1) { 
      if (questionsReply.question1 != 0) { 
       var answer_selected = questionSelected.question1; 
       if (answer_selected != undefined) { 
        $(".answer[question="+questionSelected+", answer="+answer_selected+"]").attr("checked ", "true"); 
       } 
      } 
      } 
      else if (questionSelected = 2) { 
      if (questionsReply.question2 != 0) { 
       var answer_selected = questionSelected.question2; 
       if (answer_selected != undefined) { 
        alert("Entrou aqui - 2! " + answer_selected); 
        $(".answer[question="+questionSelected+", answer="+answer_selected+"]").attr("checked ", "true"); 
       } 
      } 
      } 
      else if (questionSelected = 3) { 
      if (questionsReply.question3 != 0) { 
       var answer_selected = questionSelected.question3; 
       if (answer_selected != undefined) { 
        alert("Entrou aqui - 3! " + answer_selected); 
        $(".answer[question="+questionSelected+", answer="+answer_selected+"]").attr("checked ", "true"); 
       } 
      } 
      } 
      else if (questionSelected = 4) { 
      if (questionsReply.question4 != 0) { 
       var answer_selected = questionSelected.question4; 
       if (answer_selected != undefined) { 
        alert("Entrou aqui - 4! " + answer_selected); 
        $(".answer[question="+questionSelected+", answer="+answer_selected+"]").attr("checked ", "true"); 
       } 
      } 
      } 
      else if (questionSelected = 5) { 
      if (questionsReply.question5 != 0) { 
       var answer_selected = questionSelected.question5; 
       if (answer_selected != undefined) { 
        alert("Entrou aqui - 5! " + answer_selected); 
        $(".answer[question="+questionSelected+", answer="+answer_selected+"]").attr("checked ", "true"); 
       } 
      } 
      } 

     } 

     function colorTabs() { 
      if (questionsReply.question1 != 0) { $(".tablinks[tabid='1']").addClass("tabchanged"); } 
      if (questionsReply.question2 != 0) { $(".tablinks[tabid='2']").addClass("tabchanged"); } 
      if (questionsReply.question3 != 0) { $(".tablinks[tabid='3']").addClass("tabchanged"); } 
      if (questionsReply.question4 != 0) { $(".tablinks[tabid='4']").addClass("tabchanged"); } 
      if (questionsReply.question5 != 0) { $(".tablinks[tabid='5']").addClass("tabchanged"); } 
     } 

     document.getElementById("defaultOpen").click(); 

    </script> 

    <div class="col-md-auto col-md-offset-1 col-centered"> 
     <div class="panel panel-success"> 

      <div class="tab"> 
       <button class="tablinks active" tabid="1" type="button" onclick="openTab(event, 1)" id="defaultOpen"></button> 
       <button class="tablinks" tabid="2" type="button" onclick="openTab(event, 2)"></button> 
       <button class="tablinks" tabid="3" type="button" onclick="openTab(event, 3)"></button> 
       <button class="tablinks" tabid="4" type="button" onclick="openTab(event, 4)"></button> 
       <button class="tablinks" tabid="5" type="button" onclick="openTab(event, 5)"></button> 
      </div>    

      <div id="q1" class="tabcontent"> 
       <table class="table table-hover"> 
       <tbody> 
        <tr class="form-group"> 
         <h3 name="ques[1]" style="text-indent: 40px;"></h3> 
        </tr> 
        <tr class="form-group"> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta1" type="radio" question="1" answer="1" value="a">Question 1 - Answer 01</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta1" type="radio" question="1" answer="2" value="b">Question 1 - Answer 02</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta1" type="radio" question="1" answer="3" value="c">Question 1 - Answer 03</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta1" type="radio" question="1" answer="4" value="d">Question 1 - Answer 04</label><br> 
        </tr> 
       </tbody> 
       </table> 
       <center> 
        <button class="btn btn-default btnNext" data-direction="next" type="button" onclick="openTab(event, 2)"><span class="glyphicon glyphicon-triangle-right"></span> Next</button> 
       </center> 
       <br> 
      </div> 

      <div id="q2" class="tabcontent"> 
       <table class="table table-hover"> 
       <tbody> 
        <tr class="form-group"> 
         <h3 name="ques[2]" style="text-indent: 40px;"></h3> 
        </tr> 
        <tr class="form-group"> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta2" type="radio" question="2" answer="1" value="a">Question 2 - Answer 01</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta2" type="radio" question="2" answer="2" value="b">Question 2 - Answer 02</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta2" type="radio" question="2" answer="3" value="c">Question 2 - Answer 03</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta2" type="radio" question="2" answer="4" value="d">Question 2 - Answer 04</label><br> 
        </tr> 
       </tbody> 
       </table> 
       <center> 
        <button class="btn btn-default btnBack" data-direction="back" type="button" onclick="openTab(event, 1)"><span class="glyphicon glyphicon-triangle-left"></span> Back</button> 
        <button class="btn btn-default btnNext" data-direction="next" type="button" onclick="openTab(event, 3)"><span class="glyphicon glyphicon-triangle-right"></span> Next</button> 
       </center> 
       <br> 
      </div> 

      <div id="q3" class="tabcontent"> 
       <table class="table table-hover"> 
       <tbody> 
        <tr class="form-group"> 
         <h3 name="ques[3]" style="text-indent: 40px;"></h3> 
        </tr> 
        <tr class="form-group"> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta3" type="radio" question="3" answer="1" value="a">Question 3 - Answer 01</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta3" type="radio" question="3" answer="2" value="b">Question 3 - Answer 02</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta3" type="radio" question="3" answer="3" value="c">Question 3 - Answer 03</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta3" type="radio" question="3" answer="4" value="d">Question 3 - Answer 04</label><br> 
        </tr> 
       </tbody> 
       </table> 
       <center> 
        <button class="btn btn-default btnBack" data-direction="back" type="button" onclick="openTab(event, 2)"><span class="glyphicon glyphicon-triangle-left"></span> Back</button> 
        <button class="btn btn-default btnNext" data-direction="next" type="button" onclick="openTab(event, 4)"><span class="glyphicon glyphicon-triangle-right"></span> Next</button> 
       </center> 
       <br> 
      </div> 

      <div id="q4" class="tabcontent"> 
       <table class="table table-hover"> 
       <tbody> 
        <tr class="form-group"> 
         <h3 name="ques[4]" style="text-indent: 40px;"></h3> 
        </tr> 
        <tr class="form-group"> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta4" type="radio" question="4" answer="1" value="a">Question 4 - Answer 01</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta4" type="radio" question="4" answer="2" value="b">Question 4 - Answer 02</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta4" type="radio" question="4" answer="3" value="c">Question 4 - Answer 03</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta4" type="radio" question="4" answer="4" value="d">Question 4 - Answer 04</label><br> 
        </tr> 
       </tbody> 
       </table> 
       <center> 
        <button class="btn btn-default btnBack" data-direction="back" type="button" onclick="openTab(event, 3)"><span class="glyphicon glyphicon-triangle-left"></span> Back</button> 
        <button class="btn btn-default btnNext" data-direction="next" type="button" onclick="openTab(event, 5)"><span class="glyphicon glyphicon-triangle-right"></span> Next</button> 
       </center> 
       <br> 
      </div> 

      <div id="q5" class="tabcontent"> 
       <table class="table table-hover"> 
       <tbody> 
        <tr class="form-group"> 
         <h3 name="ques[5]" style="text-indent: 40px;"></h3> 
        </tr> 
        <tr class="form-group"> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta5" type="radio" question="5" answer="1" value="a">Question 5 - Answer 01</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta5" type="radio" question="5" answer="2" value="b">Question 5 - Answer 02</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta5" type="radio" question="5" answer="3" value="c">Question 5 - Answer 03</label><br> 
         <label class="radio-inline input-style"><input class="input-style answer" name="resposta5" type="radio" question="5" answer="4" value="d">Question 5 - Answer 04</label><br> 
        </tr> 
       </tbody> 
       </table> 
       <center> 
        <button class="btn btn-default btnBack" data-direction="back" type="button" onclick="openTab(event, 4)"><span class="glyphicon glyphicon-triangle-left"></span> Back</button> 

        <button class="btn btn-default btnSend" data-direction="back" type="button" onclick="sendResult()"><span class="glyphicon glyphicon-triangle-left"></span> SEND</button> 
       </center> 
       <br> 
      </div> 

     </div> 
    </div> 

</body> 

</html> 
+0

は、あなたのREPONSEのための先生に感謝し、それは私のために少し複雑です。データベースからの質問は、各タブに無作為に表示する必要があります。なぜ私はループを使用したのですか。 – Dza

+0

ループをそのまま使用して、コードを適応させてください。私はjsbin上で動作するのを見ることができるように削除しました! –

+0

ありがとうございます、しかし、私はまだPHPの新しい、私は上のコードを適応させるここで苦労している。 – Dza

関連する問題