1

我正在尝试将值从单选按钮发送到可观察到的淘汰赛。我究竟做错了什么?(顺便说一句,我是淘汰赛的新手..)

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>
4

1 回答 1

0

这是获得选择的答案部分:

HTML:

Question:
<ul data-bind="foreach: answers">
    <li>
        <input type="radio" name="group" data-bind="value: itemName, checked: $root.currentAnswer" /> <span data-bind="text: itemName"></span>
    </li>
</ul>
<label data-bind="text: currentAnswer()"></label>

JS/淘汰赛:

var AnswerVM = {
    answers: ko.observableArray([{
        itemName: 'Answer 1'
    }, {
        itemName: 'Answer 2'
    }]),
    currentAnswer: ko.observable()
};

ko.applyBindings(AnswerVM);

试试看:

http://jsfiddle.net/5bc66m2h/3/

希望能帮助到你!

于 2015-07-30T09:08:46.693 回答