简体   繁体   中英

Jquery Validation Rule Depends Generates Uncaught TypeError

When I try to use jQuery validation rules depends as follows:

<script>
$('#sign-up-form form').validate({
    rules: {
        state: "required",
        school: {
            depends: function(element) {
                return $("#id_school_id").val().length;
                }
            }
    }
});
</script>

Each time the depends part of the rule runs I get Uncaught TypeError: Cannot read property 'call' of undefined

The id that I'm checking is there (#id_school_id). I'm using jquery.validation.js v 1.11.1

I misunderstood depends to be a rule method. You need to apply its value to a rule method. So, for example

$('#sign-up-form form').validate({
rules: {
    state: "required",
    school: {
        required: {
            depends: function(element) {
                return $("#id_school_id").val().length;
            }
        }
    }
},
messages: {
    school: "Please choose a valid school name from the choices presented"
}
});

</script>

Whether the field 'school' is required depends on the field 'school_id' having a length.

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