【发布时间】:2015-02-11 09:23:47
【问题描述】:
我有四个组,每个组包含三个键:question、hint 和 answer。我只需要随机输出 question 和 hint 两个值,然后针对 answer 值执行它们,用户必须在其中输入文本域。我的想法是遍历整个数组,然后随机输出,最后执行。
这怎么可能? http://jsfiddle.net/vo01u94p/
jQuery/JS
$('.submit').on('click', function () {
var HISTORY;
var question_1 = {
question: 'When did Martin Luther King, Jr. die?',
hint: 'He was born on January 15, 1929 and he died at the age of 39.',
answer: 'April 4, 1968',
};
var question_2 = {
question: 'Who discovered America?',
hint: 'This person led three ships - the Nina, the Pinta and the Santa Maria - out of the Spanish port of Palos on August 3, 1492.',
answer: 'Christopher Columbus',
};
var question_3 = {
question: 'What event occured on July 4, 1776?',
hint: 'Thomas Jefferson played an important role.',
answer: 'The United States Declaration of Independence was written',
};
var question_4 = {
question: "What continent covers 8.3% of the Earth's total surface area (28.4% of its land area)?",
hint: 'Also known as the New World',
answer: 'The Americas, or America',
};
var questions = [];
questions[0] = question_1;
questions[1] = question_2;
questions[2] = question_3;
questions[3] = question_4;
var library = {
questions: questions
}
var libraries = [];
libraries[HISTORY] = library;
var questionNo = 3;
console.log(libraries[HISTORY].questions[questionNo].question);
console.log(libraries[HISTORY].questions[questionNo].hint);
console.log(libraries[HISTORY].questions[questionNo].answer);
});
【问题讨论】:
标签: javascript jquery arrays random