【问题标题】:Disable specific textbox from dynamic creation of textbox using knockout.js使用 knockout.js 从动态创建文本框中禁用特定文本框
【发布时间】:2015-03-05 09:51:05
【问题描述】:

我需要从动态创建的文本框列表中禁用特定的文本框。考虑我使用“创建按钮”动态创建 5 个文本框的代码,我需要禁用第二个文本框。

  <input type="button" value="Create TextBox" data-bind="click: addCourse"/>
  <input type="button" value="Disable 2nd TextBox" data-bind="click: disable"/>

  <div data-bind="foreach: cources">
        <div>
            <input type="text"  data-bind="value: textValue,disable: disableStatus"/>
        </div>          
  </div>
  <div data-bind="foreach: cources">
      <div>
         <span type="text" data-bind="text: textValue"/>
       </div>
   </div>

js代码:

function CourseViewModel(){
    this.textValue = ko.observable(''); 
    this.disableStatus = ko.observable(false); 
}

function CeremonyViewModel() {

    this.cources = ko.observableArray();


    this.addCourse = function(){
        this.cources.push(new CourseViewModel());
    };
    this.disable = function()
    {
        this.disableStatus(true);
    }
}

ko.applyBindings(new CeremonyViewModel());

【问题讨论】:

    标签: javascript html mvvm knockout.js


    【解决方案1】:

    只需使用数组索引:

    this.disable = function()
    {
        if (this.cources().length > 1) {
            this.cources()[1].disableStatus(true);
        }
    }
    

    http://jsfiddle.net/422b5ju7/1/

    【讨论】:

      猜你喜欢
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多