我正在尝试使用 Knockout 属性禁用选择disabled
。当我更新具有我创建的属性的第一个选择 ( dep
) 时,它必须删除对 dep 的 IDelement 的禁用。
ko.bindingHandlers.singleChosen = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
$(element).chosen();
**//Disabled if element have disableOptions default=disabled**
if(allBindingsAccessor()["disableOptions"]){
if(allBindingsAccessor()["disableOptions"].default == "disabled"){
$(element).attr('disabled', true);
}
}
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = ko.utils.unwrapObservable(valueAccessor());
//Remove disabled of the elementID on disableOptions dep:
if(allBindingsAccessor()["disableOptions"]){
if(allBindingsAccessor()["disableOptions"].dep !== ""){
var dep = allBindingsAccessor()["disableOptions"].dep;
$(document.getElementById(dep)).attr('disabled', false);
}
}
ko.bindingHandlers.options.update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
$(element).trigger("liszt:updated");
}
};
对应的HTML:
<td>
<select id="region"
data-bind="
singleChosen: newData,
optionsText: 'Name',
value: region,
disableOptions: {
default:'',
msg:'',
dep: 'pais'
}">
</select>
</td>
<td data-bind="with: region">
<select id='pais'
data-bind="
singleChosen: Countries,
optionsText: 'Name',
optionsCaption: 'Selecciona un pais',
value: $parent.country,
disableOptions: {
default:'disabled',
msg:'',
dep: ''
}">
</select>
</td>