Is there any way to disable a bunch of form elements at once?

前端 未结 5 1932
既然无缘
既然无缘 2021-02-02 14:42

I\'d like to disable a section of HTML form elements depending on some conditions. This seems to be the ideal way to do that:

<
5条回答
  •  花落未央
    2021-02-02 15:21

    Ok, I've come up with a Knockout.js specific implementation that hopefully will help some other people in the same boat. This solution could probably be adapted for other solutions and platforms with a little effort.

    First, I created a Knockout binding:

    ko.bindingHandlers.allowEdit = {
       init: function(element, valueAccessor)
       {
          if(!valueAccessor())
          {
             element.disabled = true;
             element.readOnly = true;
    
             if(element.tagName === 'FIELDSET')
             {
                $(':input', element).attr('disabled', 'disabled');
             }
          }
       }
    };
    

    Note, you'd have to implement the update method too if you wanted to allow changes to this binding. I didn't have this requirement.

    You could then use the binding as such:

提交回复
热议问题