KnockoutJS必需/禁用属性未删除

时间:2015-03-04 12:43:49

标签: javascript jquery knockout.js

所以我有一个淘汰赛原型,你动态添加输入,然后设置他们自己的设置。将其视为表单构建者就是这样。但是,我注意到残疾人和被要求的人并没有那么好。它将值设置为disabled或required但是当我将其设置为false时,它仍然保留在元素上而没有其状态,导致它仍然起作用。请任何人帮助或给予指导。

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>

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 个答案:

答案 0 :(得分:1)

你最好使用Knockout自己的内置disable绑定处理程序:

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

请参阅Fiddle

或者,您可以查看明确的&#39;真实性&#39;在您的情况下,如果条件不满足,Knockout将删除该属性。例如:

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

请参阅此Fiddle(在&#39;禁用&#39;输入中键入&#39; true&#39;以激活disabled)。