简体   繁体   English

jQuery验证方法中的this.optional()似乎不起作用

[英]this.optional() in jQuery validation method doesn't seem to work

I've got a little problem here. 我这里有一个小问题。 I've got the following rule for one of my fields: 对于我的一个领域,我有以下规则:

StartDate: {
    required: isDelayed,
    dateRU: true
}

isDelayed() returns false, so I guess StartDate field should be optional. isDelayed()返回false,因此我猜StartDate字段应该是可选的。 However if I check it inside my dateRU method: 但是,如果我在dateRU方法中检查它:

$.validator.addMethod(
    "dateRU",
    function(value, element) {
        return this.optional(element) || isValidDate($.trim(value));
    },
    "Date is incorrect"
);

this.optional(element) always returns false for StartDate. this.optional(element)对于StartDate总是返回false。 I can't figure out what's wrong. 我不知道怎么了。 Any ideas? 有任何想法吗?

UPD. UPD。 Does optional() returns true only if element is not required AND IS EMPTY ? 仅当不需要元素且为EMPTY optional()才返回true吗? 'Cause that may be my problem. 因为这可能是我的问题。

My guess would be your isDelayed function is not returning what you expect, making it required. 我的猜测是您的isDelayed函数未返回您期望的结果,因此是必需的。 I tested your code using this and it works: 我使用此代码测试了您的代码,它可以正常工作:

function isDelayed() { return false; }

You can see a full working demo here , try and see what's different in your code from the demo, my best guess is that isDelayed() method, or a difference between your actual and posted code. 您可以在此处看到完整的演示示例 ,尝试查看与演示中代码有什么不同,我的最佳猜测是isDelayed()方法,或者实际代码与发布的代码之间存在差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM