gpt4 book ai didi

javascript - 元素内部 html 未清除

转载 作者:行者123 更新时间:2023-11-28 05:20:21 24 4
gpt4 key购买 nike

我用 JavaScript 创建了一个简单的测验应用程序。

当用户选择一个选项来回答问题并提交时,上一个问题的选项不应该可见。

不幸的是,他们是,我不确定为什么。当我试图在 askQuestion() 函数中将 quizQuestion 分离到它自己的 div 中时,问题发生了。

我在下面包含了代码和 fiddle 。

https://jsfiddle.net/scrippyfingers/z84pc45t/

这是JavaScript

var allQuestions = [
{
question: "what time is it?",
choices: ["10AM", "11AM", "Hammertime"],
correctAnswer: 2
},
{
question: "what is for lunch?",
choices: ["Pizza", "Soup", "Tacos"],
correctAnswer: 0
},
{
question: "what?",
choices: ["who", "where", "how"],
correctAnswer: 1
}
];

var submitBtn = document.getElementById("myBtn");

var currentQuestion = 0;
var tally = 0;

var quizForm = document.getElementById("quiz");
var quizQuestion = document.getElementById("quizQuestion");

var question;
var choices;

var radioButtons = document.getElementsByName('radioOption');

var index = 0;

function beginQuiz(){
if(currentQuestion === 0){
submitBtn.value = "Start Quiz";
}
}

function askQuestion(){
choices = allQuestions[currentQuestion].choices;
question = allQuestions[currentQuestion].question;
if(currentQuestion < allQuestions.length){
submitBtn.value = "Next";
quizQuestion.innerHTML = "<p class='quizQuestion'>" + question + "</p>";
for(var i = 0; i < choices.length; i++){
quizForm.innerHTML += " <label class='answerChoice'><input type= 'radio' name= 'radioOption' value=' " + choices[i] + "'/>" + choices[i] + "</label>" + "<br>";
}
if(currentQuestion == allQuestions.length - 1){
submitBtn.value = "Submit";
} else if(currentQuestion > allQuestions.length - 1){
calcQuiz();
}
}
}

function lookForChecked(){
if(radioButtons.length > 1){
var checked = false;
for(var i = 0; i < radioButtons.length; i++){
var selection = radioButtons[i];

if(selection.checked){
var index = [i];
if(i === allQuestions[currentQuestion].correctAnswer){
tally++;
}
if(currentQuestion < allQuestions.length -1){
currentQuestion++;
} else {
console.log('you have ended the quiz');
calcQuiz();
return false;
}
break;
}
}
if($('input[name="radioOption"]:checked').length < 1){
alert('Please Make a Selection');
}
}
askQuestion();
}

function calcQuiz(){
quizForm.innerHTML = tally + " out of " + allQuestions.length;
// submitBtn.remove();
submitBtn.value = "Play again?";
}

window.onload = beginQuiz();
submitBtn.addEventListener('click', lookForChecked);

这是 HTML

<div class="bg1"></div>
<div id="quizQuestion"></div>
<div id="quiz"></div>
<div class="btnContainer">
<input type='submit' id='myBtn' value='' />
</div><!-- end div.btnContainer -->

最佳答案

quizForm 永远不会被清除,所以我们只是一直追加它。我在处理该 div 的循环之前添加了这个:quizForm.innerHTML = "";

function askQuestion(){
choices = allQuestions[currentQuestion].choices;
question = allQuestions[currentQuestion].question;
if(currentQuestion < allQuestions.length){
submitBtn.value = "Next";
quizQuestion.innerHTML = "<p class='quizQuestion'>" + question + "</p>";
quizForm.innerHTML = "";
for(var i = 0; i < choices.length; i++){
quizForm.innerHTML += " <label class='answerChoice'><input type= 'radio' name= 'radioOption' value=' " + choices[i] + "'/>" + choices[i] + "</label>" + "<br>";
}
if(currentQuestion == allQuestions.length - 1){
submitBtn.value = "Submit";
} else if(currentQuestion > allQuestions.length - 1){
calcQuiz();
}
} }

关于javascript - 元素内部 html 未清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39538678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com