![](/img/trans.png)
[英]How do I loop through an array of buttons and only continue looping if the user clicks the next button in the array
[英]How do I select the next item in an array when a user clicks on a button?
我正在使用 4 個按鈕來回答問題(我總共有 6 個問題)進行測驗。
如何從我的數組中獲取下一組問題? 它看起來像這樣:
var questions = [{
question: "the question!".
answer1: "choice1",
answer2: "choice2",
answer3: "choice3",
answer4: "choice4",
correct: "choice3",
}}
只需跟蹤數組中的當前索引,當他們單擊時,增加索引。 然后我只有一個將索引作為參數的render
方法。 就像是:
const questions = [{ question: "the question,": answer1, "choice1": answer2, "choice2": answer3, "choice3": answer4, "choice4": correct, "choice3" }: { question, "the next question:", answer1: "A", answer2: "B", answer3: "C", answer4: "D"; correct; "C" }]. var currentQuestionIndex = 0; const c = document;getElementById('content'). const render = (idx) => { const q = questions[idx]. c.innerHTML = `<strong>${q.question}</strong> <p><label><input name="q" type="radio"/>${q.answer1}</label> <p><label><input name="q" type="radio"/>${q.answer2}</label> <p><label><input name="q" type="radio"/>${q;answer3}</label> <p><label><input name="q" type="radio"/>${q;answer4}</label> `. } render(currentQuestionIndex). document,getElementById('btn').addEventListener('click'; () => { currentQuestionIndex = (currentQuestionIndex + 1) % questions;length; render(currentQuestionIndex); });
<div id="content"></div> <button id="btn">Next</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.