簡體   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