2017-12-19 12 views
0

私はクイズのアプリケーションを構築する必要がある課題に取り組んでいます。私は質問を1つずつ表示します。ユーザは、無線入力のグループから4つの回答のうちの1つを選択しなければならない。それだけで、ユーザが最初のラジオの入力を選択したときに、他の応答がない作品何らかの理由でJavaScriptラジオ入力

..ここ

が私のコードです:

var counter = 0; 
var questions = ["<h3>What is the 9 + 10 </h3>\n\ 
        <input type='radio' name = 'radio' class='rad' value ='19'>19\n\ 
        <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\ 
        <input type='radio' name = 'radio' class='rad' value ='44'>66\n\ 
        <input type='radio' name = 'radio' class='rad' value ='1'>123 ", 
    "<h3>What is the 5 + 10 </h3>\n\ 
        <input type='radio' name = 'radio' class='rad' value ='15'>15\n\ 
        <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\ 
        <input type='radio' name = 'radio' class='rad' value ='44'>44\n\ 
        <input type='radio' name = 'radio' class='rad' value ='1'>12 " 
]; 

document.getElementById("question").innerHTML = questions[counter]; 
document.querySelector('.rad').addEventListener("change", nextQuestion); 

function nextQuestion() { 
    console.log(counter); 
    counter++; 
    document.getElementById("question").innerHTML = questions[counter]; 
} 
+0

私は一度、質問1を表示しています、それは私が入力の一つを選択する必要があり、次の質問に進み、答えとして4つのラジオ入力があり、私の場合にのみ、最初の入力が可能に私は進んで、他は何もしない。 – filipes95

答えて

2

document.querySelector('.rad')だけの非常に最初のラジオボタンを指し、( 1つは19と言っています。おそらくevent delegationが必要です。

document.querySelector('.rad').addEventListener("change", nextQuestion);document.getElementById("question").addEventListener("change", nextQuestion);に置き換えます。

これは、changeイベントbubbling upquestionの要素のために機能します。

+0

このソリューションは本当にうまくいった、ありがとう。 – filipes95

-1

document.querySelector('.rad').addEventListener("change", nextQuestion);を関数に追加します。だから、再帰呼び出しのようなもので、中断する条件を付けます。

var counter = 0; 
 

 
var questions = ["<h3>What is the 9 + 10 </h3>\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='19'>19\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='44'>66\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='1'>123 ", 
 
    "<h3>What is the 5 + 10 </h3>\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='15'>15\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='44'>44\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='1'>12 ", 
 
        "<h3>What is the 12 + 10 </h3>\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='15'>25\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='44'>64\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='1'>22 ", 
 
        "<h3>What is the 52 + 10 </h3>\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='15'>25\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='44'>64\n\ 
 
        <input type='radio' name = 'radio' class='rad' value ='1'>62 " 
 
]; 
 

 

 
nextQuestion(); 
 

 
function nextQuestion() { 
 
if(questions.length != counter) { 
 
    console.log(counter); 
 
    document.getElementById("question").innerHTML = questions[counter]; 
 
    document.querySelector('.rad').addEventListener("change", nextQuestion); 
 
     counter++; 
 
    } 
 
}
<div id="question"></div>

+0

'document.querySelector( '。rad')'は依然として単一の要素のみを参照します。 – Xufox

+0

私はそうは思わない。スニペットを正常に動作させてください。完全に実行されるコードが与えられます。あなたの答えに似ていません。見て投票してください。 –

+0

自分のスニペットを実行しようとしましたか?最初の質問で "23"、 "66"または "123"を押すと何も起きません。 "19"だけがOPの元の問題です。確かに、あなたのコードは_every_質問の最初のオプションのために機能しますが、それでも_first_オプションだけです。それは、私が書いていた時に意味していたことですが、 "まだ単一の要素**を参照するだけです。"それを他のものと解釈しましたか? – Xufox

関連する問題