jQuery验证,使用submitHandler提交 [英] jQuery validate, submitting with submitHandler

查看:618
本文介绍了jQuery验证,使用submitHandler提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表单

<form method="POST" action="" name="myform" id="myform">
    <input type="text" name="email" id="email" />
    <input type="supmit" name="submit" id="submit"  value="submit" />
</form>

和jQuery的代码:

And a code of jQuery:

$(document).ready(function(){
    var validator = $("#myform").validate({
        ignore: ".ignore",
        rules: {...},
        messages: {...},
        submitHandler: function(form) {
            $.post('usr.php?resetpw', $(this).serialize(), function (data, textStatus) {
                form.submit();
                alert(data.inf);
            },'json');
        },
});

以及PHP代码usr.php得到$ _POST变量( isset($ _ POST ['email']) = false

and also PHP-code usr.php that doesn't get $_POST-variables (isset($_POST['email']) = false)

if(isset($_GET['resetpw'])) {
    $loggingData = array(
        'inf' => utf8_encode("Your email address is: ".$_POST['email']),
        'errorEmail' => '',
        'mailExists' => '',
        'success' => '',
    );
}
echo json_encode($loggingData);

什么是将变量发布到PHP的正确代码?

What is a correct code that post variables reached to PHP?

谢谢

推荐答案

我刚从这里检查了验证插件的来源 http://jquery.bassistance.de/validate/jquery.validate.js

I just checked the source of validate plugin from here http://jquery.bassistance.de/validate/jquery.validate.js

并且它正在调用 submitHandler 喜欢这个

and it's calling submitHandler like this

 validator.settings.submitHandler.call( validator, validator.currentForm );

这意味着这个将引用验证器对象而不是表单,因此请使用表单参数参考表格并序列化它的字段

Which means this will refer to the validator object not the form, so use the form argument to refer to the form and serialize it's fields like this

 $(form).serialize()

这篇关于jQuery验证,使用submitHandler提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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