繁体   English   中英

单击下一步按钮时问题索引未更新

[英]Question index not updating when I click next button

我编写这段代码只是因为当我单击下一个按钮时问题和答案不会更新并且 go 到数组中的下一个问题,但它会在页面加载时显示第一个问题和答案。 我不确定我在这里没有做什么。

 const questiontext= document.getElementById('question-text'); const A= document.getElementById('OptionA'); const B= document.getElementById('OptionB'); const C= document.getElementById('OptionC'); const D= document.getElementById('OptionD'); const options= document.getElementsByClassName('options'); const nextbutton= document.getElementById('next'); const submitbutton= document.getElementById('submit'); const questions=[ { question: "What is the best item at mcdonalds?", answerA: "Fries", answerB: "Big Mac", answerC: "Mcnuggets", answerD: "Quarter Pounder", correctanswer: "Big Mac" }, { question: "What is the cheapest thing on the mcdonalds menu?", answerA: "Fries", answerB: "Double Cheeseburger", answerC: "Happy Meal", answerD: "Orange juice", correctanswer: "Fries" }, { question: "What is the least popular item at mcdonalds?", answerA: "Filet O Fish", answerB: "Hamburger", answerC: "Veggie Deluxe", answerD: "Mineral water", correctanswer: "Filet O Fish" }, { question: "How many dips are you allowed with 20 Mcnuggets?", answerA: "2", answerB: "4", answerC: "3", answerD: "6", correctanswer: "4" } ]; //Question index at start const questionindex= 0; const currentquestion= () =>{ questiontext.innerHTML= questions[questionindex].question; A.innerHTML= questions[questionindex].answerA; B.innerHTML= questions[questionindex].answerB; C.innerHTML= questions[questionindex].answerC; D.innerHTML= questions[questionindex].answerD; if(questionindex === questions.length){ submitbutton.classList.remove('hidden'); } } const nextquestion= () =>{ questionindex++ } //Load first question and answers currentquestion(questionindex); //Button to display next question nextbutton.addEventListener('click', nextquestion);

改变

const questionindex 

let questionindex

更改const并将其替换为letvar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM