Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何仅在jQuery验证插件成功时发送ajax验证?_Javascript_Php_Jquery_Html - Fatal编程技术网

Javascript 如何仅在jQuery验证插件成功时发送ajax验证?

Javascript 如何仅在jQuery验证插件成功时发送ajax验证?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我使用jQuery验证插件验证表单字段。现在我只想在字段有效的情况下发送ajax请求,这意味着jQuery验证插件成功后 JavaScript: $(函数(){ $(“#表格”)。验证({ errorPlacement:函数(标签、元素){ label.addClass('arrow'); 标签。插入符(元素); }, 包装器:“span”, 规则:{ 电邮:{ 要求:正确, 电子邮件:真的 }, 姓名:{ 要求:正确, 最小长度:5 }, 密码:{ 要求:正确, 最小长度:6 }, 重新密码

我使用jQuery验证插件验证表单字段。现在我只想在字段有效的情况下发送ajax请求,这意味着jQuery验证插件成功后

JavaScript:

$(函数(){
$(“#表格”)。验证({
errorPlacement:函数(标签、元素){
label.addClass('arrow');
标签。插入符(元素);
},
包装器:“span”,
规则:{
电邮:{
要求:正确,
电子邮件:真的
},
姓名:{
要求:正确,
最小长度:5
},
密码:{
要求:正确,
最小长度:6
},
重新密码:{
要求:正确,
最小长度:6,
equalTo:“#密码”
},
出生日期:{
必填项:true
},
关于你:{
必填项:true
}
},
信息:{
电邮:{
必填:“此字段为必填字段!”,
电子邮件:“请输入有效的电子邮件!”
},
姓名:{
必填:“此字段为必填字段!”,
minlength:“名称至少需要有5个字符!”
},
密码:{
必需:“需要密码!”,
minlength:“密码至少需要有6个字符!”
},
重新密码:{
必需:“需要重新密码!”,
minlength:“rePassword至少需要6个字符!”,
equalTo:“密码不匹配!”
}
}
}); 
});

登记
$(函数(){
$(“#表单”).submit(函数(){
//仅当验证插件成功时运行此代码?
$.ajax({
方法:“张贴”,
url:“API.php”,
数据:{email:$(“#email”).val(),name:$(“#name”).val(),password:$(“#password”).val(),rePassword:$(“#rePassword”).val(),birthDate:$(“#birthDate”).val(),aboutYou:$(“#aboutYou”).val(),
成功:函数(result){$(“#div1”).html(result);
}});
});
});
电子邮件

名称

密码

重新设置密码

出生日期

关于你自己

我猜您正在使用以下插件:

你要找的可能是:

submitHandler (default: native form submit)
Type: Function()
Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit. The right place to submit a form via Ajax after it is validated.
所以你可以这样做

$(".selector").validate({
  submitHandler: function(form) {
    $(form).ajaxSubmit();
  }
});
API文档:

======================

如果这不是您正在使用的插件,请查找以下内容:

插件API文档中的“当表单有效时处理实际提交的回调”。它肯定会被实现为回调或承诺对象

将函数(错误、事件)附加到initail验证程序插件脚本中

$(function(){
   $("#form").validate({
       errorPlacement: function(label, element) {
        label.addClass('arrow');
        label.insertAfter(element);
        },
        wrapper: 'span',

       rules: {
           email: {
               required: true,
               email: true
           },
           name: {
               required: true,
               minlength: 5
           },
           password: {
               required: true,
               minlength: 6
           },
           rePassword:{
               required: true,
               minlength: 6,
               equalTo: "#password"
           },
           birthDate:{
               required: true
           },
           aboutYou:{
               required: true
           }

       },
       messages:{
           email: {
               required: "this field is required!",
               email: "please enter a valid email!"
           },
           name:{
               required: "this field is required!",
               minlength: "name needs to have at least 5 chars!"
           },
           password:{
               required: "password is required!",
               minlength: "password needs to have at least 6 chars!"
           },
           rePassword:{
               required: "rePassword is required!",
               minlength: "rePassword needs to have at least 6 chars!",
               equalTo: "passwords don't match!"
           }
       },
       function(errors, event) {
           if (!errors.length) _submit();
       }

   }); 
});



function _submit() {
    $( "#form" ).submit(function(){
        $.ajax({
            method: "post",
            url: "API.php", 
            data:{email: $("#email").val(), name: $("#name").val(), password: $("#password").val(), rePassword: $("#rePassword").val(), birthDate: $("#birthDate").val(), aboutYou: $("#aboutYou").val()},
            success: function(result){$("#div1").html(result);
         }});
     });
}