这个jsFiddle为我的目标显示了不正确的代码。我希望每次用户选择将这一天添加/删除到除日列表中的某一天。使用淘汰赛 2.1.0。
HTML:
<ul data-bind="foreach: bydayOptions">
<li>
<input type="checkbox" name="byday" data-bind="attr: {value: value, checked: $parent.byday().indexOf(value) >= 0}, event: {change: $parent.bydayHandler(value)}">
<label data-bind="for: value"><b data-bind="text: $data.text"></b></label>
</li>
</ul>
<b data-bind="text: ko.toJSON(byday())"></b>
JavaScript:
var viewModel = function(args) {
var self = this;
self.byday = ko.observableArray(args.byday);
self.bydayOptions = [{
text: "Monday",
value: "Mon"
}, {text: "Tuesday",
value: "Tue"
}, {text: "Wednesday",
value: "Wed",
}, {text: "Thursday",
value: "Thu"
}, {text: "Friday",
value: "Fri"
}, {text: "Saturday",
value: "Sat"
}, {text: "Sunday",
value: "Sun"
}];
self.bydayHandler = function(d){
console.debug(">>> inside the byday Handler");
console.log(self.byday());
if(d !== undefined){
if(self.byday().indexOf(d) >= 0){
self.byday().splice(self.byday().indexOf(d), 1);
} else {
self.byday().push(d);
}
}
};
};
ko.applyBindings(new viewModel({byday: ["Mon"]}));
</p>