我正在做一个是/否问卷,一次回答一个问题,在问题的底部有一个“下一步”按钮。

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

 <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] = "";

07-24 18:05