首页 > 解决方案 > 每次按下下一个问题按钮时,我都想将其更改为数组中写入的内容

问题描述

所以我在 HTML 中有 4 个区域:问题、注释、Button1、Button2。我有一个对象,每个对象都有自己的数组中的值:

var list = {
    1: ['This is question 1', 'These are notes 1', 'this is answer 1A', 'this is answer 1B'],
    2: ['This is question 2', 'These are notes 2', 'this is answer 2A', 'this is answer 2B']

}; 

当我单击下一个问题按钮时,我希望将值更改为对象的下一行这是我的函数:

var i = 0;

function nextQuestion() {
    i += 1;    
    document.getElementById('question').innerHTML = list.i[0];
    document.getElementById('notes').innerHTML = list.i[1];
    document.getElementById('btn1').value = list.i[2];
    document.getElementById('btn2').value = list.i[3]; 
}

我知道我很接近,但我是一个初学者,不知道我需要在这里做什么。

也许是一个循环???

谢谢

标签: javascriptarraysobjectfor-loop

解决方案


更改list.i[0];list[i][0];它应该可以工作。


推荐阅读