如何将复选框值绑定到对象上的 Knockout observableArray?

How to bind checkbox value to Knockout observableArray on an object?

我有这样的东西:

var ScheduleDay = function(day, times) {
    var self = this;
    self.day = ko.observable(day || "");
    self.times = ko.observableArray(times || []);
};

function viewModel() {
    var self = this;
    self.schedule = ko.observableArray([
        new ScheduleDay("Monday"),
        new ScheduleDay("Tuesday"),
        new ScheduleDay("Wednesday"),
        new ScheduleDay("Thursday"),
        new ScheduleDay("Friday"),
        new ScheduleDay("Saturday"),
        new ScheduleDay("Sunday")
    ])
}

ko.applyBindings(new viewModel());

<input id="mondaySchedule" type="checkbox" data-bind="checked ???">

一周中的每一天我都有 3 个复选框:早上、下午和晚上。我想要一个类似这样的模型:时间表:{星期一:["morning","afternoon"],星期二:["afternoon"],等等}

由于您在评论中指出了您的问题,我建议您看一下这个简单的标记,它将满足您的要求:

<ul data-bind="foreach: schedule">
    <li>
        <strong data-bind="text: day"></strong>
        <input type="checkbox" data-bind="checked: times" value='morning' /> Morning
        <input type="checkbox" data-bind="checked: times" value='afternoon' /> Afternoon
        <input type="checkbox" data-bind="checked: times" value='evening' /> Evening
    </li>
</ul>

工作示例:http://jsfiddle.net/qjzbossb/