KnockoutJS Required / Disabled attributes not remo

2019-08-11 14:55发布

问题:

So I have a knockout prototype where you add inputs dynamically and then set each of their own settings. Think of its as a form builder is such. However, I noticed that disabled and required doesn't work that great. It sets the value to disabled or required but when I turn it into false it still remains on the element without its state, causing it to still function. Please can anyone help or give guidance.

HTML

<div class="leftpanel">
                <div class="input-row" data-bind="foreach: inputItems">                 
                    <div class="input-row-item">                
                        <div class="input-item">    
                            <label data-bind="text: label"></label>                 
                            <input data-bind="attr:{ name: name, placeholder: placeholder, disabled: disabled, value: value, type: type }">                 
                        </div>
                        <div class="input-settings">
                            <input type="text" class="nb-remove" data-bind="value: label" placeholder="input label">
                            <input type="text" value="text" class="nb-remove" data-bind="value: type" placeholder="input type">
                            <input type="text" class="nb-remove" data-bind="value: name" placeholder="input name">
                            <input type="text" class="nb-remove" data-bind="value: placeholder" placeholder="input placeholder">
                            <input type="text" class="nb-remove" data-bind="value: disabled" placeholder="input disabled">
                            <input type="text" class="nb-remove" data-bind="value: value" placeholder="input value">
                        </div>
                    </div>                  
                </div>          
            </div>
            <div class="rightpanel">
                Here be draggables!
                <br/>
                <button data-bind="click: addInput">ADD TEXT INPUT</button>
            </div>

The JS

$(function(){
            var InputItem = function InputItem(label, type, name, placeholder, disabled, value) {
                this.label          = ko.observable(label);
                this.type           = ko.observable(type);
                this.name           = ko.observable(name);
                this.placeholder    = ko.observable(placeholder);
                this.disabled       = ko.observable(disabled);
                this.value          = ko.observable(value);
            }

            var ViewModel = function ViewModel() {
              var that = this;

              this.inputItems = ko.observableArray([]);

              this.addInput = function addInput() {
                that.inputItems.push(new InputItem());
              };

            }

            ko.applyBindings(new ViewModel());
        });

回答1:

You better use Knockout's own built-in disable binding-handler:

<input data-bind="disable: disabled, attr: { name: name, placeholder: placeholder, value: value, type: type }" />

See Fiddle

Alternatively, you can check for explicit 'truthfulness' in your condition so that Knockout will remove the attribute if the condition is not met. For example:

<input data-bind="attr: { disabled: disabled() === 'true', ...}" />

See this Fiddle (type 'true' in the 'disabled' input to activate disabled).