ajax防止表单自动提交
重写表单的checkForm方法,并用if和else解决异步判断的问题。
function checkForm(){
1 var flag = false; 2 3 $.ajaxSetup({async : false}); 4 5 $.post(<?php echo "'".$SITE_PHP.'captchaAjax.php'."'";?>,'captcha='+document.getElementById("captcha").value,function(msg){if(!parseInt(msg)){layer.alert('验证码错误!',{time:1000});return flag;}else{ 6 7 $.post(<?php echo "'".$SITE_PHP.'loginNamePassAjax.php'."'";?>,'username='+document.getElementById("username").value+'&'+'password='+document.getElementById("password").value,function(msg){if(!parseInt(msg)){layer.alert('用户名或密码错误!',{time:1000});return flag;}else{ 8 9 flag = true;return flag;}});}}); 10 11 return flag;
}