具有“禁用”问题的级联DropDown

时间:2013-04-30 08:40:26

标签: jquery knockout.js

我正在尝试使用Knockout属性disabled禁用select。当我更新第一个具有我所做的属性(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>

1 个答案:

答案 0 :(得分:1)

替换此行

  $(document.getElementById(dep)).attr('disabled', false);

  $(document.getElementById(dep)).removeAttr('disabled');

disabled属性不需要值。默认情况下,当您指定禁用时,该项将被禁用。