如何在Knockout.js中更改复选框选中的值?

时间:2014-10-14 10:50:44

标签: events checkbox knockout.js binding checked

我想通过jQuery更改复选框值,但Knockout绑定无法正常工作

var viewModel = {
    myValue: ko.observable(true)
};

ko.applyBindings(viewModel);

$(':checkbox').prop({checked: false}).change();

http://jsfiddle.net/stereodenis/n7zxvu01/3/

1 个答案:

答案 0 :(得分:1)

Knockout.js文档中描述了它:checked binding

<p>Send me spam: <input type="checkbox" data-bind="checked: wantsSpam" /></p>

<script type="text/javascript">
var viewModel = {
    wantsSpam: ko.observable(true) // Initially checked
};

// ... then later ...
viewModel.wantsSpam(false); // The checkbox becomes unchecked
</script>