埃洛,
我已经尝试了所有选项,以使用optgropus制作一个(多个)selectbox,并用 knockout 绑定(bind)options/selectedOptions。
selectedOptions绑定(bind)似乎存在问题。我的数据似乎合法,但不会显示预选的选项。
我在JSFiddle中做了一个例子:http://jsfiddle.net/aCS7D/251/
<select data-bind="selectedOptions: selectedOptions" multiple="multiple">
<option>Please choose an option</option>
<optgroup data-bind="repeat: groups" data-repeat-bind="attr: {label: $item().label}">
<option data-bind="repeat: $item().children" data-repeat-bind="text: $item().label, option: $item()"></option>
</optgroup>
使用一个选定的选项,它可以工作,但是具有多个selectedoptions,则选择框不能正确呈现它们。
如果有人能解决这个问题,您将是我的英雄!
最佳答案
您可以在应用诸如this之类的绑定(bind)之后推送它们:
this.selectedOptions = ko.observableArray([]);
//The single selected option
this.selectedOption = ko.observable(selected1);
var vm = new ViewModel()
ko.applyBindings(vm);
var selected1 = vm.groups()[1].children()[1];
var selected2 = vm.groups()[1].children()[0];
vm.selectedOptions.push(selected1);
vm.selectedOptions.push(selected2);
关于jquery - 在带有optgroup的select中使用selectedOptions knockout 绑定(bind)预先选择多个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22810506/