jQuery默认不停止执行

jQuery默认不停止执行,jquery,preventdefault,Jquery,Preventdefault,我应该先粘贴代码 $(document).ready(function(){ $("#submit").click(function(e){ $(".error").hide(); var hasError = false; .... .... var captcha = $("#captchacode").val(); if(cap

我应该先粘贴代码

$(document).ready(function(){
    $("#submit").click(function(e){                                    
        $(".error").hide();
        var hasError = false;

        .... ....

        var captcha = $("#captchacode").val();
        if(captcha == '') {
            $("#captchacode").after('<span class="error">You forgot to enter security image.</span>');
            hasError = true;
        } else {

            $.post("/checkcaptcha.php",
                { captchac: captcha},
                    function(data){

                        if (data == "Invalid"){
                            $("#captchacode").after('<span class="error">Invalid security text. Please try again</span>');
                            hasError = true;
                            e.preventDefault();
                        }
                    }
            );
        }


        if(hasError == false) {
            $(this).hide();
            $("#sendEmail li.buttons").append('<img src="images/loading.gif" alt="Loading" id="loading" />');

            $.post("/sendemail.php",
                { emailFrom: emailFromVal, name: name, message: messageVal },
                    function(data){
                        $("#sendEmail").slideUp("normal", function() {                 

                            $("#sendEmail").before('<h1>Success</h1><p>Your email was sent.</p>');                                          
                        });
                    }
                 );
        }

        return false;
    });                        
});
$(文档).ready(函数(){
$(“#提交”)。点击(功能(e){
$(“.error”).hide();
var hasError=false;
.... ....
var captcha=$(“#captchacode”).val();
如果(验证码=“”){
$(“#captchacode”)。在('您忘记输入安全映像')之后;
hasrerror=true;
}否则{
$.post(“/checkcaptcha.php”,
{captchac:captcha},
功能(数据){
如果(数据==“无效”){
$(“#captchacode”)。在('无效的安全文本。请重试')之后;
hasrerror=true;
e、 预防默认值();
}
}
);
}
if(hasError==false){
$(this.hide();
$(“#sendmail li.buttons”).append(“”);
$.post(“/sendmail.php”,
{emailFrom:emailFromVal,name:name,message:messageVal},
功能(数据){
$(“#sendmail”).slideUp(“正常”,函数(){
$(“#发送电子邮件”)。之前('Success您的电子邮件已发送。

'); }); } ); } 返回false; }); });
一切正常。正在发送电子邮件。我在输入无效的captcha值时收到错误消息。在显示错误消息之后,将进入下一步,表单将提交。我需要你的建议来解决这个问题

谢谢。

jQuery.post()函数是AJAX调用的简写。AJAX调用是异步的,通过传递请求完成时执行的回调来工作。调用
$.post()
不会在请求收到响应之前停止代码的执行,因为AJAX的全部目的是让不会发生。这意味着您的
if(hasrerror==false)
行将在回调函数的
if(data==“Invalid”)
部分之前执行

如果希望在AJAX调用响应之后发生某些事情,则需要将其移动到回调函数:

$(document).ready(function () {
    $("#submit").click(function (e) {
        $(".error").hide();
        var hasError = false,
            elem = this;

        ........

        var captcha = $("#captchacode").val();
        if (captcha == '') {
            $("#captchacode").after('<span class="error">You forgot to enter security image.</span>');
            hasError = true;
        } else {

            $.post("/checkcaptcha.php", {
                captchac: captcha
            },

            function (data) {

                if (data == "Invalid") {
                    $("#captchacode").after('<span class="error">Invalid security text. Please try again</span>');
                    hasError = true;
                    e.preventDefault();
                } else {
                    $(elem).hide();
                    $("#sendEmail li.buttons").append('<img src="images/loading.gif" alt="Loading" id="loading" />');

                    $.post("/sendemail.php", {
                        emailFrom: emailFromVal,
                        name: name,
                        message: messageVal
                    },

                    function (data) {
                        $("#sendEmail").slideUp("normal", function () {

                            $("#sendEmail").before('<h1>Success</h1><p>Your email was sent.</p>');
                        });
                    });
                }
            });
        }
        return false;
    });
});
$(文档).ready(函数(){
$(“#提交”)。单击(功能(e){
$(“.error”).hide();
var hasError=false,
elem=这个;
........
var captcha=$(“#captchacode”).val();
如果(验证码=“”){
$(“#captchacode”)。在('您忘记输入安全映像')之后;
hasrerror=true;
}否则{
$.post(“/checkcaptcha.php”{
验证码
},
功能(数据){
如果(数据==“无效”){
$(“#captchacode”)。在('无效的安全文本。请重试')之后;
hasrerror=true;
e、 预防默认值();
}否则{
$(elem.hide();
$(“#sendmail li.buttons”).append(“”);
$.post(“/sendmail.php”{
emailFrom:emailFromVal,
姓名:姓名,,
消息:messageVal
},
功能(数据){
$(“发送电子邮件”).slideUp(“正常”,函数(){
$(“#发送电子邮件”)。之前('Success您的电子邮件已发送。

'); }); }); } }); } 返回false; }); });