KnockoutValidation和条件必需规则 [英] KnockoutValidation and the conditional required rule
本文介绍了KnockoutValidation和条件必需规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用条件语句 KnockoutValidation 。请参阅以下代码:
I am trying to use KnockoutValidation with conditional statements. See code below:
self.transactionType = ko.observable('Option1');
self.ConditionalField = ko.observable().extend({
required: true,
onlyIf: self.transactionType = ="Option2"
});
不幸的是,这不起作用。我希望只有 transactionType
的值'Option2'
。
Unfortunately this doesn't work. I want to have ConditionalField
only required if transactionType
has value 'Option2'
.
使用knockout.validation.js条件验证的最佳方法是什么?
What is the best way to use conditional validation with knockout.validation.js?
推荐答案
我已经解决了。
首先,在我定义了后,我错误地宣布
。有效的结束代码如下所示: transactiontype
conditionalfield
First of all I made the mistake of declaring the transactiontype
after I had defined the conditionalfield
. The end code that works looks like this:
self.transactionType = ko.observable("Option1");
self.conditionalField = ko.observable().extend({
required: {
onlyIf: function () {
return self.transactionType () == "Option2";
}
}
});
这篇关于KnockoutValidation和条件必需规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文