誰能幫我激活第一個單選按鈕作爲標準如何激活單選按鈕
這裏是我的代碼
<div>Wiederholung</div>
<div data-bind="foreach:answers">
<label>
<input type="radio" name="wiederholung" data-bind="click: $root.setChosenAnswer" />
<span data-bind="text: name"></span>
</label>
<br />
</div>
<pre data-bind="text: JSON.stringify(ko.toJS($root), null, 2)"></pre>
JS:
function Question() {
var self = this;
this.answers = ko.observableArray([
new Answer("Nie", true),
new Answer("Täglich", false),
new Answer("Wöchentlich", false)]);
this.setChosenAnswer = function(wahl) {
if (wahl !== self.userAnswer()) {
ko.utils.arrayForEach(self.answers(), function(answer) {
answer.isChosen(wahl === answer);
});
self.userAnswer(wahl);
}
return true;
};
this.userAnswer = ko.observable();
this.userAnswer.subscribe(function(newValue) {
alert("Ich moechte mein Termin " + this.userAnswer().name() + " Wiederholen");
}, this);
}
function Answer(name, isChosen) {
this.name = ko.observable(name);
this.isChosen = ko.observable(isChosen);
}
ko.applyBindings(new Question());