我的代码中所选值的范围最初似乎没有填充。谁能告诉我这是为什么?
小提琴:http: //jsfiddle.net/vVLdQ/
我的 HTML:
<div>
Disable:
<span data-bind="text: options[1].text"></span><input type="checkbox" data-bind="checked: options[1].disable" />
<span data-bind="text: options[2].text"></span><input type="checkbox" data-bind="checked: options[2].disable" />
<span data-bind="text: options[3].text"></span><input type="checkbox" data-bind="checked: options[3].disable" />
</div>
<div>
<select data-bind="value: selectedValue">
<option data-bind="value: options[0].text, text: options[0].text"></option>
<option data-bind="value: options[1].text, text: options[1].text, disable: options[1].disable"></option>
<option data-bind="value: options[2].text, text: options[2].text, disable: options[2].disable"></option>
<option data-bind="value: options[3].text, text: options[3].text, disable: options[3].disable"></option>
</select>
</div>
<span data-bind="text: selectedValue"></span>
我的Javascript:
var selectModel;
function OptionModel(text) {
var me = this;
me.text = text;
me.disable = ko.observable(false);
me.disable.subscribe(function(disableVal) {
if (disableVal && selectModel.selectedValue() == me.text) {
selectModel.selectedValue('-- Select --');
}
});
}
var options = [
new OptionModel('-- Select --'),
new OptionModel('a'),
new OptionModel('b'),
new OptionModel('c')
];
selectModel = {
options: options,
selectedValue: ko.observable(options[0].text)
};
ko.applyBindings(selectModel);