2

我正在接受用户的问题并使用 Lawnchair 将它们保存在 DOM 中。声明的第一步

var dbQuestions = new Lawnchair({name:'questions'}, function(obj) { consol('questions initialized'); }); 

用户可以输入QuestionText, ChoiceNextQuestionID然后点击保存。然后每个问题都转换为以下格式

var survey= {   
"name": "Internet Based survey",
"sortOrder":10,
"questions" : [
    { "QuestionID": "1", "SurveyID":1, "QuestionText":"Excuse me Sir/Madame, do you have 60 seconds to answer a few questions today?", "SortOrder":"10", "Choices": [{ "ChoiceID":"1", "Choice":"Yes", "NextQuestionID":"3", "InitiateSurvey":"true" }, 
                      { "ChoiceID":"2", "Choice":"No", "NextQuestionID":"1" }, 
                      { "ChoiceID":"3", "Choice":"What is it About?", "NextQuestionID":"2" }] 
    }]};

并保存

dbQuestions.save({key:'Q',value:survey},function(obj) {
    consol('Question Saved successfully');

});

保存第一个问题后,用户将输入下一个问题。上述方法工作正常,但我需要以不同的方式保存。假设用户保存 3 个问题,我想以以下格式存储,以便我可以轻松访问问题。

var survey = {  
"name": "Internet Based survey",
"sortOrder":10,
"questions" : [
    { "QuestionID": "1", "SurveyID":1, "QuestionText":"Excuse me Sir/Madame, do you have 60 seconds to answer a few questions today?", "SortOrder":"10", "Choices": [{ "ChoiceID":"1", "Choice":"Yes", "NextQuestionID":"3", "InitiateSurvey":"true" }, 
                      { "ChoiceID":"2", "Choice":"No", "NextQuestionID":"1" }, 
                      { "ChoiceID":"3", "Choice":"What is it About?", "NextQuestionID":"2" }] 
    }, 
    { "QuestionID": "2", "SurveyID":1, "QuestionText":"It is about a way to earn a little extra money with a side business", "SortOrder":"15", "Choices":[{ "ChoiceID":"1", "Choice":"Yes", "NextQuestionID":"3", "InitiateSurvey":"true" }, 
               { "ChoiceID":"2", "Choice":"No", "NextQuestionID":"1" }] 
    }, 
    { "QuestionID": "3", "SurveyID":1, "QuestionText":"Are you on the internet?", "SortOrder":"10", 
        "Choices":[{ "ChoiceID":"4", "Choice":"Yes", "NextQuestionID":"4" }, 
                   { "ChoiceID":"5", "Choice":"No", "NextQuestionID":"1" }] 
    }]};

所以我的问题是如何保存每个问题将保存在问题属性下的问题。

提前致谢。

4

1 回答 1

2

你可以试试这个

var index = 0; // number of question
$.each(previousData.question,function(){
       finalArray[index] = previousData.question[index]; //contain array
       index++; //here index is number of question
});

finalArray[index] = data.question;
data = {'question': finalArray }; // convert array to object
于 2014-02-13T12:01:34.583 回答