我正在接受用户的问题,并使用Lawnchair将其保存在DOM中。声明的第一步
var dbQuestions = new Lawnchair({name:'questions'}, function(obj) { consol('questions initialized'); });
用户可以输入
QuestionText
,Choice
,NextQuestionID
并单击保存,然后将每个问题转换为以下格式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" }]
}]};
所以我的问题是如何保存问题,每个问题将保存在问题属性下。
提前致谢。
最佳答案
你可以试试这个
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