gpt4 book ai didi

javascript onclick 从数组中按顺序

转载 作者:行者123 更新时间:2023-12-03 12:18:53 24 4
gpt4 key购买 nike

我正在制作一份“是/否”调查问卷,一次只回答一个问题,问题底部有一个“下一步”按钮。

我希望每次单击“下一步”按钮时都会循环到数组中的下一个问题。我该怎么做?

 <button class="btn btn-large btn-block btn-primary" type="button"   onclick="nextQuestion()">Next</button>

function nextQuestion(){
document.getElementById("questionArea").innerHTML = ???,

}

var questionSet = new Array();
questionSet[0] = "";
questionSet[1] = "";
questionSet[2] = "";
questionSet[3] = "";

最佳答案

只需添加一个 var 整数索引并在单击下一步时增加它

 <button class="btn btn-large btn-block btn-primary" type="button"onclick="nextQuestion()">Next</button>
var index_question = 0;
function nextQuestion(){
document.getElementById("questionArea").innerHTML = questionSet[index_question];
index_question ++;
}

var questionSet = new Array();
questionSet[0] = "";
questionSet[1] = "";
questionSet[2] = "";
questionSet[3] = "";

关于javascript onclick 从数组中按顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24537023/

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