2017-11-03 3 views
-1

私は友達のクイズに取り組んでいます。これは初めてのJSプロジェクトです。クイズアプリのラジオボタンをオフにしようとしています

解決策を見つけるのに苦労していることの1つは、ユーザーが「回答を送信」をクリックした後にラジオボタンのチェックを外す方法です。現時点では、チェックされたラジオボタンに対して条件をfalseに戻すためにforループを使用していますが、それはうまく動作しません。

誰かがこのコードを見て、私が間違っていることを助けたり指摘したりすることはできますか?

事前に感謝します。

$(document).ready(function(){ 

var azeem = [ 
{ 
question: "What is Azeem's favourte color?", 
choices: ["Blue", "Yellow", "Red", "Green"], 
answer: 0 
}, 
{ 
question: "What is Azeem's favourte movie?", 
choices: ["Scarface", "The Terminator", "Shawshank Redemption", "The Dark Knight"], 
answer: 3 
}, 
{ 
question: "What was Azeem's first ever job role?", 
choices: ["Cleaner", "Store Assistant", "Sales", "Admin"], 
answer: 1 
}, 
{ 
question: "What is Azeem's favourite dish?", 
choices: ["Pasta", "Pizza", "Chips", "Curry"], 
answer: 0 
}, 
{ 
question: "What subject did Azeem enjoy the most in school?", 
choices: ["Drama", "Science", "P.E", "History"], 
answer: 0 
}, 
{ 
question: "What subject did Azeem least enjoy in school?", 
choices: ["Geography", "Maths", "History", "I.T"], 
answer: 1 
}, 
{ 
question: "Which one of these cities has Azeem travelled to?", 
choices: ["Madrid", "Lisbon", "Istanbul", "Dublin"], 
answer: 1 
}, 
{ 
question: "Which college did Azeem study in?", 
choices: ["NewVic", "Redbridge", "East Ham", "Barking"], 
answer: 3 
}, 
{ 
question: "Who is Azeem's favourite sports icon?", 
choices: ["Eric Cantona", "Muhammad Ali", "Cristiano Ronaldo", "Prince Naseem"], 
answer: 1 
}, 
{ 
question: "Who is Azeem's favourite music artist?", 
choices: ["Michael Jackson", "Eminem", "Drake", "Linkin Park"], 
answer: 1 
}, 

]; 

var correctAnswers = 0; 
var currentQuestion = 0; 
var questionNumberCounter = 1; 
var questionNumber = document.getElementById("questionCount"); 
var choices = document.getElementById("choicesSection"); 
var questions = document.getElementById("ques"); 
var radioButtons = document.getElementsByName("answer"); 
questions.innerText = azeem[currentQuestion].question; 


// The following event listener will transition from the instructions to the first question of the quiz 

      document.getElementById("startquiz").addEventListener("click",function(){ 
$(".quiz-intro").fadeOut(600); 
$(".quiz-section").delay(600).slideDown("slow"); 
questionNumber.innerText = questionNumberCounter; 
azeem[currentQuestion].choices.forEach(function(value, index){ 
var radio = document.createElement("input"); 
var label = document.createElement("label"); 
var div = document.createElement("div"); 
$(div).addClass("choice"); 
radio.setAttribute("type", "radio"); 
radio.setAttribute("name", "answer"); 
radio.setAttribute("value", index); 
label.innerHTML = value +"<br>"; 
choices.appendChild(div); 
div.appendChild(radio); 
div.appendChild(label); 

}) 

}) 


      document.getElementById("submitanswer").addEventListener("click",function(){ 

if (!$('input[name=answer]:checked').length > 0){ 
alert("Please select an aswer"); 
return; 
} 

var labels = document.getElementsByTagName("label"); 
questionNumberCounter++; 
questionNumber.innerText = questionNumberCounter; 
currentQuestion++ 
questions.innerText = azeem[currentQuestion].question; 
azeem[currentQuestion].choices.forEach(function(value, ind) { 
labels[ind].innerText = value; 
}); 

var radioValue = $("input[name='answer']:checked").val(); 
if (radioValue === azeem[currentQuestion].answer){ 
correctAnswers++; 
} 

for (var i = 0; i < radioButtons.length; i++){ 
if (radioButtons[i].checked === true){ 
radioButtons[i].checked === false; 
} 
} 

}) 

     }) 
+0

まずエラーを修正する必要があります。 –

答えて

2

このライン: ラジオボタン[i]は.checkedを=== falseは、

する必要があります。 radioButtons [i] .checked = false;

乾杯。

+0

ありがとうございましたDeveloper Nodejs !!!! :) – jimmy118

+0

喜んでjimmy118を助ける –

関連する問題