-1

下面的 ajax 成功函数只显示我最后一个 error4 消息。我正在检查的回复正常,但未显示消息。

$.ajax({
    type:...
    url :...
    data:...
success: function (exst_rsp) {
//if email is not register
    if (exst_rsp == "email_no") {
        exstInfo.text("error1");
        exstInfo.addClass("error");
        email_err = true;
    }
    else {
        exstInfo.text("");
        exstInfo.removeClass("error");
        email_err = false;
    }

//if user has already submit a coupon
    if (exst_rsp == "played") {
        couponInfo.text("error2");
        couponInfo.addClass("error");
        email_pl_err = true;
    }
    else {
        couponInfo.text("");
        couponInfo.removeClass("error");
        email_pl_err = false;
    }

//if email & pass don't match
    if (exst_rsp == "email_pass_no") {
        matchInfo.text("error3");
        matchInfo.addClass("error");
        pass_err = true;
    }
    else {
        matchInfo.text("");
        matchInfo.removeClass("error");
        pass_err = false;
    }

//if captcha don't match
    if (exst_rsp == "wrong") {
        capthInfo.text("error4");
        capthInfo.addClass("error");
        captcha_err = true;
    }
    else {
        capthInfo.text("");
        capthInfo.removeClass("error");
        captcha_err = false;
    }

    if (exst_rsp == "ok") {
        exst_rsp_ok = false;
        $('#content').load('f.php');
    }
    else {
        exst_rsp_ok = true;
    }
    if (email_reg_err == true || email_pl_err == true || email_pass_err == true || captcha_err == true || exst_rsp_ok == true) {
        return false;
    }
    else {
        return true;
    }
}
})
4

2 回答 2

2

您的代码可以使用一些重构,并且可以使用更简洁的功能完成您想要的功能,但是为了给您一个工作示例,您可以从这里开始。

http://jsfiddle.net/Q3Zpk/1/

您可以更改数组中的值以查看相应的 html 更改。

我将错误放在一个数组中,以便让您抢占先机,但我会考虑使用错误代码而不是字符串和一个函数,该函数将该数组传递给它并相应地解析错误。

于 2012-07-18T17:57:04.863 回答
0

错误是我使用相同的 ID 来显示错误。代码已更正。

并且工作正常!

谢谢

于 2012-07-20T06:52:19.737 回答