为什么jQuery Validate" remote:"验证需要一个函数来设置数据值吗? [英] Why does jQuery Validate "remote:" validation require a function to set data values?

查看:86
本文介绍了为什么jQuery Validate" remote:"验证需要一个函数来设置数据值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery Validate文档中,它显示了使用远程验证调用将其他值传递到服务器端验证脚本/资源的示例:


http://docs.jquery.com/Plugins/Validation/Methods/remote #code


以下是示例代码:

  $(#myform)。validate({
规则:{
email:{
required:true,
email:true,
remote:{
url:check-email.php,
类型:post,
数据:{
用户名:function(){
返回$(#username)。val();
}
}
}
}
}
});

为什么我需要使用函数来设置用户名数据参数?:

 用户名:function(){
返回$(#username) .VAL();
}

而不仅仅是:

 用户名:$(#username)。val()

我已经尝试了两种方法,果然第二种方法没有分配用户名:一个值。



我确实在远程方法函数的jQuery Validate代码中设置了断点:

  remote:function(value,element,param){
...
}

试图了解这里发生了什么,但我真的不是更聪明的。

解决方案

jQuery.fn.validate(config ) 用于设置验证规则,而不是实际执行验证。整个 {rules:/ * * /} 对象将在发生这种情况时进行准确评估。






如果包含

  $('#username')。val()

在rules对象中,表达式将立即进行评估,并保存当时元素的值。






如果您改为传递

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

作为值,当 远程验证器 <$ c $时,调用>并进行AJAX调用c> jQuery.param data 属性展平为字符串。



<请注意,此验证器将其整个配置对象合并到它所进行的每个AJAX调用中,因此可以通过这种方式设置普通ajax设置对象中可接受的所有其他选项,而不仅仅是 data


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:

http://docs.jquery.com/Plugins/Validation/Methods/remote#code

Here's the sample code:

$("#myform").validate({
  rules: {
    email: {
      required: true,
      email: true,
      remote: {
        url: "check-email.php",
        type: "post",
        data: {
          username: function() {
            return $("#username").val();
          }
        }
      }
    }
  }
});

Why do I need to use a function to set the username data parameter?:

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

rather than just:

username: $("#username").val()

I've tried both methods and sure enough the second method doesn't assign username: a value.

I did set a breakpoint in the jQuery Validate code in the remote method function:

remote: function(value, element, param) {
    ...
}

to try and understand what is happening here, but I am really none-the-wiser.

解决方案

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.

这篇关于为什么jQuery Validate&quot; remote:&quot;验证需要一个函数来设置数据值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆