gpt4 book ai didi

Javascript:测验程序:不加载第二个问题

转载 作者:行者123 更新时间:2023-11-28 16:57:55 24 4
gpt4 key购买 nike

作为学习过程的一部分,我正在学习 javascript 并编写一个测验程序。

<!DOCTYPE html> 
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form type="submit" onsubmit = "populateQuestion()">
<h2 id="question">Question goes here</h2>
<input name = "option" type="radio" value="A"><span id = "choiceA">First option here</span><br/>
<input name = "option" type="radio" value="B"><span id = "choiceB">Second option here</span><br/>
<input name = "option" type="radio" value="C"><span id = "choiceC">Third option here</span><br/>
<input name = "option" type="radio" value="D"><span id = "choiceD">Fourth option here</span><br/>
<button type = "submit">Submit</button>
</form>
</body>

<script>
var question = document.getElementById("question");
var choiceA = document.getElementById("choiceA");
var choiceB = document.getElementById("choiceB");
var choiceC = document.getElementById("choiceC");
var choiceD = document.getElementById("choiceD");

var quesIndex = 0;
var defQuestions = [
{
question : "Q01?",
choiceA : "A1",
choiceB : "B1",
choiceC : "C1",
choiceD : "D1",
answer : "A"
},
{
question : "Q02?",
choiceA : "A2",
choiceB : "B2",
choiceC : "C2",
choiceD : "D2",
answer : "A"

},
{
question : "Q03?",
choiceA : "A3",
choiceB : "B3",
choiceC : "C3",
choiceD : "D3",
answer : "B"

},
{
question : "Q04?",
choiceA : "A4",
choiceB : "B4",
choiceC : "C4",
choiceD : "D4",
answer : "B"

}
];

function renderQuestion() {
question.innerHTML = defQuestions[quesIndex].question;
choiceA.innerHTML = defQuestions[quesIndex].choiceA;
choiceB.innerHTML = defQuestions[quesIndex].choiceB;
choiceC.innerHTML = defQuestions[quesIndex].choiceC;
choiceD.innerHTML = defQuestions[quesIndex].choiceD;
}

function populateQuestion() {
console.log(quesIndex);
renderQuestion();
quesIndex += 1;
}

populateQuestion();
</script>
</html>

当我加载页面时,populateQuestion() 执行并加载默认问题和选项(defQuestions 数组的第一个元素)。这部分工作正常。

Image

当我单击提交按钮时,应该加载下一个问题。但下一个问题未加载。

console.log(quesIndex); 仅打印 0,然后控制台窗口记录清除。

执行过程中有什么问题吗?

最佳答案

我不确定您打算如何处理表单提交,但您当前的实现将提交表单,从而导致页面重新加载并重置脚本。

您应该添加一个事件监听器来捕获表单提交事件并自行处理(我希望您将用户给出的答案存储在数组中)

var question = document.getElementById("question");
var choiceA = document.getElementById("choiceA");
var choiceB = document.getElementById("choiceB");
var choiceC = document.getElementById("choiceC");
var choiceD = document.getElementById("choiceD");

var quesIndex = 0;
var defQuestions = [{
question: "Q01?",
choiceA: "A1",
choiceB: "B1",
choiceC: "C1",
choiceD: "D1",
answer: "A"
},
{
question: "Q02?",
choiceA: "A2",
choiceB: "B2",
choiceC: "C2",
choiceD: "D2",
answer: "A"

},
{
question: "Q03?",
choiceA: "A3",
choiceB: "B3",
choiceC: "C3",
choiceD: "D3",
answer: "B"

},
{
question: "Q04?",
choiceA: "A4",
choiceB: "B4",
choiceC: "C4",
choiceD: "D4",
answer: "B"

}
];


var questionnaire = document.getElementById("questionnaire");

function renderQuestion() {
question.innerHTML = defQuestions[quesIndex].question;
choiceA.innerHTML = defQuestions[quesIndex].choiceA;
choiceB.innerHTML = defQuestions[quesIndex].choiceB;
choiceC.innerHTML = defQuestions[quesIndex].choiceC;
choiceD.innerHTML = defQuestions[quesIndex].choiceD;
}

function populateQuestion() {
console.log(quesIndex);
renderQuestion();
quesIndex += 1;
}

function onsubmit(e) {
e.preventDefault(); //prevent form from actually posting
var a = questionnaire.querySelector('input[name = option]:checked');

console.clear();
console.log("Q:",quesIndex,"Answer:",a.value);
a.checked=false;//deselect it ready for new question
populateQuestion();
}

questionnaire.addEventListener('submit', onsubmit, false);


populateQuestion();
form {
margin-bottom:100px;/* just some space for the console log in the snippet*/
}
<form id="questionnaire" type="submit">
<h2 id="question">Question goes here</h2>
<input name="option" type="radio" value="A"><span id="choiceA">First option here</span><br/>
<input name="option" type="radio" value="B"><span id="choiceB">Second option here</span><br/>
<input name="option" type="radio" value="C"><span id="choiceC">Third option here</span><br/>
<input name="option" type="radio" value="D" required><span id="choiceD">Fourth option here</span><br/>
<button type="submit">Submit</button>
</form>

关于Javascript:测验程序:不加载第二个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580857/

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