2017-12-31 28 views
0
function buildQuiz(currentIndex) { 

    if (currentIndex === 0) { 
    quiz.forEach((item, index) => { 
     outputAnswers = []; 
     for (option in item.option) { 
     outputAnswers.push(
      `<label> <input type="radio" name="question${index}" value="${option}"> 
             ${item.option[option]} </label><br>` 
     ); 
     } 

     // add this question and its answers to the output 
     output.push(
     `<div class="slide${index}"> 
      <div class="question"> ${item.question} </div> 
      <div class="answers"> ${outputAnswers.join("")} </div> 
     </div>` 
    ); 
    }); 
    } 

私はJavaScriptでクイズのアプリケーションを作成していますが、ループで1つずつdivを追加しています。個々の「div」にはラジオボタン付きの質問とそのオプションも含まれています。私は、ラジオボタンを実際にチェックしているラジオボタンを読むことに苦労していますか?あなたはラジオボタンにクラスを割り当てて、クリック/変更ハンドラを登録することができますラジオボタンがHTMLではなくJavascriptで宣言されている場合、ラジオボタンの読み方はJavascriptでチェックされているかどうか?

+0

あなたは、変数クイズやダミーデータのためのデータを提供することができます...可能性が可変のクイズのためのデータだけ他の作業スニペットを提供する場合... –

+0

ここでjqueryを使用していますか?ライブラリは何ですか? –

+0

https://csunix.mohawkcollege.ca/~000736376/a4/ –

答えて

0

document.querySelectorAll(".your-class") 
    .addEventListener("click", function(e) { 
    // `e.target` will point to the radio button clicked 
    }); 

さて、あなたはをクリックして、正確にそれを識別するために、そのname財産を守ったラジオボタンを取得することができます。

0

あなたはこの方法を使用することができます:

if(your_radio.checked){ 
    // Do something 
} 
else if(!your_radio.checked){ 
    // Do something 
} 
関連する問題