用于表单验证的 jQuery post 函数只允许 alert() 显示返回的数据


jQuery post function for form validation only allows alert() to show returned data

好的,所以我几乎尝试了一切。

$.post("include/ajax.php", { type: "workbookNumber", wbn: $("input[name='wbn']").val() }, function(data) {
    error.push("<li>"+data+"</li>");
    alert(data);
});

error.push 是已创建的错误数组,它工作正常,但根本不会添加到数组中。 就好像那行代码不存在一样。 在逗号变量数据中有一些实例,其中有一个额外的逗号,表明它在那里,但即便如此,<li></li>仍然应该显示。

jQuery.ajax({
    type: "POST",
    url: "include/ajax.php",
    dataType:"html",
    data: { type: "workbookNumber", wbn: $("input[name='wbn']").val() },
    success:function(response) {
    alert(response);
    },
    error:function (xhr, ajaxOptions, thrownError) {
        alert("damn. -_-");
        alert(xhr.status);
        alert(thrownError);
    }    
});

我想不出任何合理的解释,我认为 PHP 没有足够的时间来显示结果,因为它正在搜索超过 20000 个代码的数据库,但是,结果仍然通过警报返回,只是没有通过可以在屏幕上显示的实际文本中的错误数组。

错误数组工作正常,只是这个函数不起作用。 以下是正常工作的其他一些示例:

if($("input[name='fname']").val() == "") {
    error.push("<li>The first name field is blank</li>");
}
if($("input[name='lname']").val() == "") {
    error.push("<li>The last name field is blank</li>");
}
if($("select[name='usertype']").val() == 0) {
    if($("input[name='vcode']").val() == "") {
        error.push("<li>The voucher code field is blank</li>");
    } else {
        $.post("include/ajax.php", { type: "findVoucher", vcode: $("input[name='vcode']").val() }, function(data) {
            if(data == "none") {
                error.push("<li>The voucher code does not exist</li>");
            }
        });
    }
}

这是整个代码:

$(document).ready(function() {
$("#sit_date").datepicker();
$("select[name='usertype']").change(function() {
    if($(this).val()=="0") {
        $(".indv").slideUp(500);
        $(".comp").slideDown(500);
    } else {
        $(".indv").slideDown(500);
        $(".comp").slideUp(500);
    }
});
$("input[name='marka'],input[name='markb']").bind("keypress paste keyup  blur focus", function() {
    var marka = $("input[name='marka']").val();
    var markb = $("input[name='markb']").val();
    var perc = (marka/markb)*100;
    if(perc>0 && perc<=100) {
        $("#per").html(Math.round(perc));
    } else {
        $("#per").html("");
    }
});
$("input[name='vcode']").bind("keypress keyup paste blur focus", function() {
    $.post("include/ajax.php", { type: "checkVoucher", vcode: $(this).val() }, function(data) {
        $("input[name='group']").val(data);
    });
    $.post("include/ajax.php", { type: "checkType", vcode: $(this).val() }, function(data) {
        $("input[name='certificates']").val(data);
    });
});
$("input[name='wbn']").bind("keypress keyup paste blur focus", function() {
    $.post("include/ajax.php", { type: "getAssessment", wbn: $(this).val() }, function(data) {
        if(data!="") {
            $("select[name='assessment']").html(data);
        }
    });
});
/*
//turn into function
$(document).keyup(function(event){
    if(event.keyCode == 13){
        alert("works");
        $("input[name='manual_add']").click();
    }
});
*/
var error = [];
$("input[name='manual_add']").click(function() {
    if($("input[name='fname']").val() == "") {
        error.push("<li>The first name field is blank</li>");
    }
    if($("input[name='lname']").val() == "") {
        error.push("<li>The last name field is blank</li>");
    }
    if($("select[name='usertype']").val() == 0) {
        if($("input[name='vcode']").val() == "") {
            error.push("<li>The voucher code field is blank</li>");
        } else {
            $.post("include/ajax.php", { type: "findVoucher", vcode: $("input[name='vcode']").val() }, function(data) {
                if(data == "none") {
                    error.push("<li>The voucher code does not exist</li>");
                }
            });
        }
    }
    if($("input[name='wbn']").val() == "") {
        error.push("<li>The workbook number field is blank</li>");
    } else {
        $.post("include/ajax.php", { type: "workbookNumber", wbn: $("input[name='wbn']").val() }, function(data) {
            error.push("<li>"+data+"</li>");
            //this is the only thing that works correctly:
            alert(data);
        });
    }
    if(($("input[name='questions']").val() == "") && ($("input[name='marka']").val() != $("input[name='markb']").val())) {
        error.push("<li>The questions wrong field is blank</li>");
    }
    var list = "";
    $.each(error, function(i,val) { 
        list += val;
    }); 
    if(error.length>0) {
        $(".error").slideUp(500);
        $(".jquery-error ul").html("").append(list);
        $(".jquery-error").slideDown(500);
    } else {
        $("form").submit();
    }
});
});

var errorclick() 函数中声明,因此无法在该函数外部访问。全局声明它,您的代码应该可以工作。(在带有全局error变量的 jsfiddle 上对我来说工作得很好。

错误处理代码的其余部分工作正常,因为它与error变量(click()函数)在同一范围内定义。但是对 ajax 请求的回调不是在函数的上下文中执行的,而是在window上下文中执行的。这绝对是一个范围问题。

当然,您必须等待服务器的响应返回以更新错误通知。编写一个遍历 error 数组并显示相应通知的函数,然后从 AJAX 调用的error函数调用该函数。

请尝试以下操作:

var error = [];
jQuery.ajax({
    type: "POST",
    url: "include/ajax.php",
    dataType:"html",
    data: { type: "workbookNumber", wbn: $("input[name='wbn']").val() },
    success:function(response) {
        alert(response);
    },
    error:function (xhr, ajaxOptions, thrownError) {
        error.push("<li>"+thrownError+"</li>");
        showErrors();
    }    
});​
function showErrors () {
     var list = "";
    $.each(error, function(i,val) { 
        list += val;
    }); 
    if(error.length>0) {
        $(".error").slideUp(500);
        $(".jquery-error ul").html("").append(list);
        $(".jquery-error").slideDown(500);
    } else {
        $("form").submit();
    }
}

你需要等到 ajax 函数完成,如下所示:

} else { //use a variable for ajax
    var XHR = $.post("include/ajax.php", { type: "workbookNumber", wbn: $("input[name='wbn']").val() }, function(data) {
        error.push("<li>"+data+"</li>");
    });
}
XHR.always(function() { //make sure ajax is completed and array updated first
    var list = "";
    $.each(error, function(i,val) { 
        list += val;
    }); 
    if(error.length>0) {
        $(".error").slideUp(500);
        $(".jquery-error ul").html("").append(list);
        $(".jquery-error").slideDown(500);
    } else {
        $("form").submit();
    }
});

您确定没有用局部变量隐藏全局变量错误吗?尝试使用

console.log(data);
console.log(error);

在你的匿名函数中,你会看到你的问题在哪里。