2013-10-27 37 views
0

因此,對於一個學校作業,我必須使用javascript進行互動測驗。我所做的問題是,我需要將所有問題存儲在本地存儲的外部文本文件中。 我的問題存儲在一個對象數組中,我用'for'循環將它們添加到單選按鈕中我使用了一個Google搜索引擎,但是我沒有找到一個答案,使它對我來說足夠清晰。 。 我明白,我可以讀文件,但我不知道我將如何提問添加到現有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/

請幫助!

+0

當您說「本地存儲」時,您是指本地硬盤上的文件嗎? localStorage(Chrome中使用的名稱)的工作方式是它會記住與頁面相關的信息。例如。如果你做了'localStorage ['a'] = 10':即使你關閉並重新打開那個頁面,鉻現在將有一個可以localStorage ['a']檢索的變量。 – Rhyono

回答

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

在一個單獨的文件,然後按照this stackoverflow到JSON.parse(jsonData)再次獲得JSON數據出來。