Valid vs Validate

后端 未结 2 599
孤街浪徒
孤街浪徒 2021-01-04 09:42

I am trying to learn jQuery and have stumbled across a strange problem (perhaps strange only to me). So here goes: I have a form with id mainform in my app. Now

相关标签:
2条回答
  • 2021-01-04 10:18

    Assuming you are using Jquery validate library.

    Docs saying

    validate() needs to be called on the form before checking it using valid() method.

    Not .valid() before validate()

    http://jqueryvalidation.org/valid/

    http://jqueryvalidation.org/validate

    0 讨论(0)
  • 2021-01-04 10:24

    There are several differences between valid and validate. Interestingly, although the docs state that

    "validate needs to be called on the form before checking it using this method"

    this isn't actually the case, as valid calls validate() anyway.

    The two major differences are

    1. If you want to pass options into the plugin, you must call validate({...})
    2. validate() doesn't highlight any errors, whereas valid() does. You could say that valid performs 'eager' validation whereas validate sets up a 'lazy' validation, basically if you call validate() you won't see any immediate change on the page, whereas with valid() you might.
    3. valid can be called on a subset of form elements, whereas validate must be called on the form itself:

      $('form').validate({/* options here */});

      $('.myfields').valid()

    0 讨论(0)
提交回复
热议问题