0

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

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

我对 JS 还是很陌生,不知道如何实现

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

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/

请帮忙!

4

1 回答 1

0

将这部分代码放入:

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
}];

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

于 2013-10-28T00:29:10.167 回答