我需要从动态创建的文本框列表中禁用特定文本框。考虑我使用“创建按钮”动态创建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 code:
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());
答案 0 :(得分:0)
只需使用数组索引:
this.disable = function()
{
if (this.cources().length > 1) {
this.cources()[1].disableStatus(true);
}
}