【问题标题】:storing quiz questions in local storage将测验问题存储在本地存储中
【发布时间】:2013-10-28 08:29:10
【问题描述】:

因此,对于学校作业,我必须使用 javascript 进行交互式测验。我这样做了,问题是我需要将所有问题存储在本地存储的外部文本文件中。 我的问题存储在一个对象数组中,我将它们添加到带有“for”循环的单选按钮

我用谷歌搜索了它,但我没有找到一个对我来说足够清楚的答案...... 我知道我可以读出文件,但我不知道如何将问题添加到现有的 for 循环中

我还是 JS 的新手,不知道如何实现这个

这是一个代码 sn-p,我从数组中取出问题,并使用单选按钮将它们添加到列表中

var i = 0;
var len = allQuestions.length;

function frageStellen(i){
var anzahlVarianten = allQuestions[i].choices.length;


    for(var j = 0; j < anzahlVarianten; j++){


    //create radio buttons

    var option = document.createElement("li");
    var variante = document.createElement("input");
    variante.setAttribute('type', 'radio');
    variante.setAttribute('name', 'gestellteFrage'+ i);
    option.setAttribute('class', '.option');
    //fragen-Inhalt
    var inhalt = document.createTextNode(allQuestions[i].choices[j]);


    myOptions.appendChild(option);
    option.appendChild(variante);
    option.appendChild(inhalt);
    myQuestion.innerHTML = allQuestions[i].question;
    }
}

这里是完整代码的链接:http://jsfiddle.net/7HaCz/

请帮忙!

【问题讨论】:

  • 当您说“本地存储”时,您是指本地硬盘上的文件吗? localStorage(Chrome 中使用的名称)的工作方式是它会记住与页面相关的信息。例如。如果你这样做localStorage['a'] = 10:即使你关闭并重新打开该页面,chrome 现在也会有一个可作为 localStorage['a'] 检索的变量。

标签: javascript local-storage


【解决方案1】:

把这部分代码放进去:

var jsonData = [{
  question: "What is Javascript?",
  choices: ["An ancient language", "A programming language", "A medieval manuscript", "An internet troll"],
  correctAnswer: 1
}, {
  question: "Where is Venice?",
  choices: ["Peru", "Greece", "US", "Italy", "Congo"],
  correctAnswer: 3
}, {
  question: "What does RGB mean?",
  choices: ["Red, Green, Blue", "Real Graphics Body", "SWAG"],
  correctAnswer: 0
}, {
  question: "How many strings has a  guitar?",
  choices: [3, 4, 5, 6, 7, 8],
  correctAnswer: 3
}];

在一个单独的文件中,然后按照this stackoverflow 到 JSON.parse(jsonData) 再次从中取出 JSON 数据。

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多