KnockoutJS - 绑定包含optgroup和Javascript对象的select的值
KnockoutJS - 绑定包含optgroup和Javascript对象的select的值
我在这里找到了一个使用KnockoutJS创建具有optgroup的下拉列表的例子。这个例子很好,但我想将下拉列表的值绑定到我的自定义javascript对象,然后访问该对象的特定属性:
function Group(label, children) { this.label = ko.observable(label); this.children = ko.observableArray(children); } function Option(label, property) { this.label = ko.observable(label); this.someOtherProperty = ko.observable(property); } var viewModel = { groups: ko.observableArray([ new Group("Group 1", [ new Option("Option 1", "A"), new Option("Option 2", "B"), new Option("Option 3", "C") ]), new Group("Group 2", [ new Option("Option 4", "D"), new Option("Option 5", "E"), new Option("Option 6", "F") ]) ]), selectedOption: ko.observable(), specialProperty: ko.computed(function(){ this.selectedOption().someOtherProperty(); }) }; ko.applyBindings(viewModel);