Why does jQuery Validate “remote:” validation require a function to set data values?

前端 未结 1 383
小蘑菇
小蘑菇 2020-12-21 17:02

In the jQuery Validate docs it shows an example of using a remote validation call to pass additional values to server side validation script/resource:

1条回答
  •  时光说笑
    2020-12-21 17:53

    jQuery.fn.validate(config) is used to set up the validation rules, not actually perform validation. The entire {rules: /* */ } object is evaluated exactly when that happens.


    If you include

    $('#username').val()
    

    in the rules object, that expression will be evaluated immediately and the element's value at that moment will be saved.


    If you instead pass

    function() { 
      return $('#username').val(); 
    }
    

    as the value, this function will be evaluated when the remote validator is invoked and the AJAX call is made, when jQuery.param flattens the data property into a string.

    Note that this validator merges its entire config object into every AJAX call it makes, so all other options acceptable in the normal ajax settings object can be set this way, not just data.

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