<分区>
我有一些测验问题存储在我使用 AJAX 调用的 JSON 中,然后作为变量 allQuestns 存储:
// Variable stores all quiz questions
var allQuestns;
// AJAX request to retrieve quiz questions from external JSON
$.ajax({
async: false,
type: 'GET',
url: 'https://api.myjson.com/bins/dqt3f',
data: { get_param: 'value' },
dataType: 'json',
success: function (data) {
allQuestns = data;
},
error: function () {
alert("Unable to locate the quiz questions. Try again soon and contact admin - quiz@palmerhinchliffe.net")
}
});
每个测验的问题作为一个对象存储在 JSON 中,该对象引用该测验(qz1、qz2、qz3),每个对象都包含一个问题数组,每个问题数组包含一个问题字符串、一个选项数组, 以及正确的选择。
我想创建一个变量,我可以使用它来代替直接引用 JSON 中的测验对象。所以不是:
alert(allQuestns.qz2[2].question) //This retrieves the question text
//from the 3rd question of the 2nd
//quiz
我想使用一个变化的变量来引用测验编号,如下所示:
var quizCounter = "qz2";
alert(allQuestns.quizCounter[2].question);
使用 quizCounter 变量引用对象似乎不起作用。有没有办法做到这一点,或者我必须实际命名对象本身而不是创建一个可以更改的引用。
我希望能够通过使用 quizCounter 为每个测验显示正确的问题,而不是为每个测验使用单独的 html div 和 JS 函数