简体   繁体   English

当用户单击按钮时,如何 select 数组中的下一个项目?

[英]How do I select the next item in an array when a user clicks on a button?

I am making quiz using 4 buttons to answer the questions (I have 6 questions in total).我正在使用 4 个按钮来回答问题(我总共有 6 个问题)进行测验。

How do I get the next set of questions from my array?如何从我的数组中获取下一组问题? It looks like this:它看起来像这样:

var questions = [{
       question: "the question!".
       answer1: "choice1",
       answer2: "choice2",
       answer3: "choice3",
       answer4: "choice4",
       correct: "choice3",

}}

Just track the current index in the array, and when they click, increment the index.只需跟踪数组中的当前索引,当他们单击时,增加索引。 And then I'd just have a render method which takes the index as a parameter.然后我只有一个将索引作为参数的render方法。 Something like:就像是:

 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.

相关问题 如何循环遍历按钮数组,并且只有在用户单击数组中的下一个按钮时才继续循环 - 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 prevent cheating when user clicks a button 一旦用户单击 select 项目之一,我如何从 javascript 中删除 select 项目 - How do i delete a select item from javascript once a user clicks on one of the select items 用户单击按钮时如何存储类名称,以便下次访问时可以将其用作默认名称? - How can I store a class name when a user clicks on a button so it can be used as a default on next visit? 当用户单击选择标记上的提交时,如何使“选择一个”选项不通过? - How do I make the “select one” option not go through when the user clicks submit on the select tag? 当用户单击按钮时,如何解构只有数据的数组? - how can i destructure an array that only has data when the user clicks the button? 用户单击DIV时如何复制onclick按钮结果? - How do I replicate onclick button results when a user clicks a DIV? 当用户单击菜单按钮时,如何弹出菜单? - How do I make a menu pop up when the user clicks the menu button? 当用户单击按钮时,如何保存可重复使用的React组件? - How do i make my reusable react component saves when the user clicks button? 当用户单击其他按钮时,如何取消选择整个单选按钮组? - How do I deselect a whole group of radio buttons when a user clicks on some other button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM