我有ko.observableArrays驗證項目。用戶可以將項目標記爲已刪除。當它被標記爲已刪除時,我需要禁用該項目的驗證。Knockout驗證禁用驗證
如何動態禁用驗證?
例子:http://jsfiddle.net/3RZjT/2/
<div data-bind="foreach: names">
<input data-bind="value: name, valueUpdate: 'afterkeydown'" /> <a data-bind="click: deleteMe, text:deleted()?'undelete':'delete'" href="#">delete</a><br/>
</div>
function Person(name){
var self = this;
self.name = ko.observable(name).extend({ required: true});
self.deleted = ko.observable(false);
self.deleteMe = function(){
self.deleted(!self.deleted());
self.deleted.extend({ validatable: !self.deleted()});
};
}
var viewModel = {
names: ko.observableArray([new Person("Ken"), new Person("")])
};
ko.applyBindings(viewModel);
請提供您當前的代碼。 – Tomalak
我並沒有嘗試過,因爲我不知道該怎麼嘗試。我已使用@Anders答案更新了一個示例,但它對我無效。 – Homer
你應該在'deleteMe'函數中寫'self.name.extend({validatable:!self.deleted()});'。 – nemesv