gpt4 book ai didi

knockout.js - knockout 验证禁用验证

转载 作者:行者123 更新时间:2023-12-03 16:41:13 27 4
gpt4 key购买 nike

我有带有项目验证的ko.observableArrays。用户可以将项目标记为已删除。将其标记为已删除时,我需要在该项目上禁用验证。

您如何动态禁用验证?

示例: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);

最佳答案

更新
旧答案不适用于Validation的Knockout-Contrib版本(即开发活跃的分支)

https://github.com/Knockout-Contrib/Knockout-Validation

使用onlyIf选项,例如

this.enable = ko.observable(true);
this.required = ko.observable().extend({ required: { onlyIf: this.enable } });

旧答案

尝试
this.property.extend({ validatable: false }); // disables all validation for property

或者
this.property.extend({ required: false }); // disables required validation for property

关于knockout.js - knockout 验证禁用验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13400063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com