php 带有ajax提交处理程序的jquery验证插件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20597739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery validation plugin with ajax submit handler not working
提问by user756659
I've used the jquery validation plugin quite a bit in the last few days, but have yet to use it with an ajax submit. What I have is below cut down to two fields. There are no errors for the values when submitting. There is no submission happening whatsoever when clicking the submit button. It just does nothing.
在过去的几天里,我经常使用 jquery 验证插件,但还没有将它与 ajax 提交一起使用。我所拥有的是以下分为两个领域。提交时值没有错误。单击提交按钮时,不会发生任何提交。它什么都不做。
HTML:
HTML:
<form id="account-info-form" action="/process/p_profile_info.php" method="post">
<div class="row margin-bottom-20">
<div class="col-md-6 form-group">
<label>First Name</label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user fa-fw"></i>
</span>
<input class="form-control" type="text" name="fname"/>
</div>
</div>
<div class="col-md-6 form-group">
<label>Last Name</label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user fa-fw"></i>
</span>
<input class="form-control" type="text" name="lname"/>
</div>
</div>
</div>
<div class="row margin-bottom-30">
<div class="col-md-12">
<button class="btn btn-primary" type="submit" name="account-info" value="save"><i class="fa fa-check-circle"></i> Save Changes</button>
<button class="btn btn-default" type="reset">Cancel</button>
</div>
</div>
</form>
JS:
JS:
$('#account-info-form').validate({
// ajax submit
submitHandler: function (form) {
var $form = $(this);
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
dataType : 'json'
})
.done(function (response) {
if (response.success == 'success')
{
alert('success');
}
else
{
alert('fail');
}
});
return false; // required to block normal submit since you used ajax
}
});
回答by Sparky
There is no reason to do this, (and $(this)is not what you're expecting it to be)...
没有理由这样做,(并且$(this)不是您期望的那样)......
var $form = $(this);
Simply use the formargument that's passed into the function.
只需使用form传递给函数的参数即可。
submitHandler: function (form) {
$.ajax({
type: $(form).attr('method'),
url: $(form).attr('action'),
data: $(form).serialize(),
dataType : 'json'
})
.done(function (response) {
if (response.success == 'success') {
alert('success');
} else {
alert('fail');
}
});
return false; // required to block normal submit since you used ajax
}

