我正在尝试将值从单选按钮发送到可观察到的淘汰赛。我究竟做错了什么?(顺便说一句,我是淘汰赛的新手..)
function Question(data) {
this.question= ko.observable(data.question);
this.answers = ko.observableArray(data.answers);
this.explenation = ko.observable(data.explenation );
}
function QuestionViewModel() {
// Data
var self = this;
self.questoin= ko.observable();
var questionNumber = 0;
self.currentAnswer = ko.observable();
self.nextQuestion = function() {
$.getJSON("json/quiz.json", function(data) {
var newQuestion = new Question(data[questionNumber]);
self.question(newQuestion );
});
questionNumber++;
}
self.setCurrentAnswer = function(){
self.currentAnswer = currentAnswerValue;
}
}
ko.applyBindings(new QuestionViewModel());
<ul data-bind="foreach: question">
<li>
<text data-bind="text: question">
</li>
<li>
<ul data-bind="foreach: answers">
<li>
<input type="radio" name="radioAnswer" data-bind="checked: $root.setCurrentAnswer, value: currentAnswerValue">
<text data-bind="text: answerValue"></text>
</li>
</ul>
</li>
</ul>
“currentAnswerValue”是 'true' 或 'false' 并使用 json 设置。(值是正确的,当我尝试时它起作用):
<text data-bind="text: currentAnswerValue"></text>