我是knockout.js的新手。 我想要做的是让教师创建问题并将这些问题保存到json文件中,该文件稍后将在另一个应用程序中用户在randon上显示问题。最后一部分有效。如果我有一个包含数据的json文件,那么应用程序会加载数据。 我想将几个问题添加到json文件中。 所以,我真的不太明白如何使用淘汰赛。 这是我使用的代码,当然它不起作用。
html是:
<form id="addQuestion" data-bind="submit: save">
<div>
<label for="question">Enter question text here:</label>
<input id="question" name="question" type="text" data-bind="value: question"/><br>
<label for="answer1">Enter answer 1 or distractor here: </label>
<input id="answer1" name="answer1" type="text" data-bind="value: answer1'"/><br>
<label for="answer">Enter answer 2 or distractor here: </label>
<input id="answer2" name="answer2" type="text" data-bind="value: answer2"/> <br>
<label for="answer">Enter answer 3 or distractor here: </label>
<input id="answer3" name="answer3" type="text" data-bind="value: answer3"/> <br>
<label for="`question">Enter answer 4 or distractor here: </label>
<input id="answer4" name="answer4" type="text" data-bind="value: answer4"/> <br>
<label for="correctAnswer">Enter correct answer number here: </label>
<input id="correctAnswer" name="correctAnswer" type="text" data-bind="value: correctAnswer"/> <br>
<label for="image">Enter the image name: </label>
<input id="image" name="image" type="text" data-bind="value: image"> <br>
<label for="feedbackInfo">Enter feedback info here: </label>
<input id="feedbackInfo" name="feedbackInfo" type="text" data-bind="value: feedbackInfo"> <br>
<button type="button" data-bind="click: save">Submit</button>
</div>
</form>`<form id="addQuestion" data-bind="submit: save">
代码是:
function viewModel(){
var self = this;
self.question = ko.observable();
self.answer1 = ko.observable();
self.answer2 = ko.observable();
self.answer3 = ko.observable();
self.answer4 = ko.observable();
self.correctAnswer = ko.observable();
self.image = ko.observable();
self.feedbackInfo = ko.observable();
// send data for json file
self.save = function(){
$.ajax('libs/data', {
data: ko.toJSON({
question: self.question,
answer1: self.answer1,
answer2: self.answer2,
answer3: self.answer3,
answer4: self.answer4,
correctAnswer: self.correctAnswer,
image: self.image,
feedbackInfo: self.feedbackInfo
}),
type:POST,
contentType:"application/json",
success: function(result){alert(result)}
});
};
};
ko.applyBindings(viewModel);