简体   繁体   中英

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. However if I check it inside my dateRU method:

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

this.optional(element) always returns false for StartDate. I can't figure out what's wrong. Any ideas?

UPD. Does optional() returns true only if element is not required AND IS EMPTY ? 'Cause that may be my problem.

My guess would be your isDelayed function is not returning what you expect, making it required. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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