试图取消选中测验应用上的单选按钮

时间:2017-11-03 22:43:19

标签: javascript jquery html

我正在做一个朋友测验。这是我的第一个JS项目。

我正在努力寻找解决方案的一个方面是如何在用户点击“提交答案”后取消选中单选按钮。目前我正在使用for循环来尝试将已设置的单选按钮的条件设置为false,但它不起作用。

是否有人能够查看此代码并协助或指出我做错了什么?

非常感谢提前!

$(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;
}
}

})

        })

1 个答案:

答案 0 :(得分:2)

这一行: radioButtons [i] .checked === false;

应该是: radioButtons [i] .checked = false;

干杯。