jQuery的验证之前,AJAX提交 [英] jQuery validation before AJAX submit
问题描述
我有以下jQuery验证code一个简单的注册表单:
$(文件)。就绪(函数(){
$(#registerForm)。验证({
规则:{
用户名:{要求:真实,MINLENGTH:6},
密码:{要求:真实,MINLENGTH:6},
re_Password:{要求:真实,equalTo:#Password来}
},
});
});
它验证正确之前,我提交表单。
当我想也是做一个AJAX提交表单的,因为形式将不再验证并提交未经验证的问题就来了:
<形式ID =registerForm行动=register.php方法=邮报的onsubmit =xmlhttpPost('register.php','registerForm','signup-盒子);返回false;>
这是工作的默认值是:
<形式方法=邮报ID =registerForm行动=register.php>
请,如果任何人都可以点我在正确的方向,或给我一些出发点,以解决这个问题,我将不胜感激。
感谢。
由于Kundan辛格Chouhan我已经找到了解决方案添加以下code到的document.ready块:
$(#registerForm)。递交(函数(){
如果($(#registerForm)。有效()){
xmlhttpPost('register.php','registerForm,注册箱');
}
返回false;
});
我觉得你应该从你的表单标签移除onsubmit事件和的document.ready编写下面的脚本()
$(形式)。递交(函数(){
如果($(形式)。确认()){
xmlhttpPost('register.php','registerForm,注册箱');
}
返回false;
});
希望这将解决您的问题。
I have a simple signup form with the following jQuery validation code:
$(document).ready(function(){
$("#registerForm").validate({
rules: {
Username: {required: true, minlength: 6},
Password: {required: true, minlength: 6},
re_Password: {required: true, equalTo: "#Password"}
},
});
});
It validates correctly before I submit the form.
The problem comes when I want to also do an AJAX submit of the form, because the form no longer validates and submits without validation:
<form id="registerForm" action="register.php" method="post" onsubmit="xmlhttpPost('register.php', 'registerForm', 'signup-box'); return false;">
The default that works is:
<form method="post" id="registerForm" action="register.php">
Please, if anyone can point me in the right direction or give me some starting points to solve this I would be grateful.
Thanks.
Thanks to Kundan Singh Chouhan I have found the solution adding the following code to the document.ready block:
$("#registerForm").submit(function(){
if($("#registerForm").valid()){
xmlhttpPost('register.php', 'registerForm', 'signup-box');
}
return false;
});
I think you should have to remove the onsubmit event from your form tag and write the below script in document.ready()
$("form").submit(function(){
if($("form").validate()){
xmlhttpPost('register.php', 'registerForm', 'signup-box');
}
return false;
});
Hope this will fix your problem.
这篇关于jQuery的验证之前,AJAX提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!