knockout checkbox 全选
knockout checkbox 全选
<input type=checkbox data-bind="checked:IsAll"/>全选 <ul data-bind="foreach: report"> <li><input type="checkbox" name="re" data-bind="value:key,checked:$parent.All" /> <span data-bind="text: name"></span></li> </ul>
viewModel = { report: ko.observableArray([]),All: ko.observableArray([]), IsAll: ko.observable(false)};
通过订阅机制
$(function () { ko.applyBindings(viewModel); viewModel.IsAll.subscribe(function () { if (!viewModel.IsAll()) { viewModel.All.removeAll(); } else { viewModel.All.removeAll(); $.each(viewModel.Current(), function (i, j) { viewModel.All.push(j); }) } }, null, "change"); })
添加
viewModel.report.push({ key: SSO.key, name: SSO.value });