我是 KO 新手,一步一步学习。
我想根据选择的单选按钮值切换 DOM 可见性。我的代码没有按预期工作,请帮助....
HTML
<div>
Wanna see what is next?
<p><label><input type="radio" name="MyChoice" value="True" data-bind="checked: MyChoiceValue" /> Yes</label>
<label><input type="radio" name="MyChoice" value="False" data-bind="checked: MyChoiceValue" /> No</label></p>
</div>
<div data-bind="visible: isVisible">
<h1>Hello World !</h1>
</div>
和我的Javascript
viewModel = function() {
var self = this;
self.isVisible: ko.observable(''),
self.MyChoiceValue: function() {
if(self.MyChoiceValue() === 'True') {
self.isVisible(true);
} else {
self.isVisible(false);
}
}
};
ko.applyBindings(new viewModel);
jsFiddle: