我有一個非常簡單的JavaScript的表單驗證,檢查我的表單域。該函數或者構建一個errorList變量並返回false,如果該變量爲空,則該函數返回true並提交。如何訂購此jQuery運行ajax然後必須返回true
我已經爲此驗證功能添加了一個新圖層。我現在想調用一個外部函數,該函數只需發送一個包含所有表單驗證的Ajax請求,然後通過電子郵件發送給我,說明用戶是否通過了表單驗證或者沒有。
當表單不驗證時調用這個外部函數就沒事了,因爲當我返回false時;顯然表單不提交,從而允許Ajax結束。
我只是無法弄清楚如何觸發外部函數,讓它完成,然後返回true並讓表單提交。它只是跳過了ajax請求。
有人可以幫助邏輯?
驗證功能:
if (errorList.length > 0) {
// Log the errors in quote.shared.js:
logValidationErrors("N/A - Pre Quote", $("#prequote-form"), "productname", errorList);
$("#error-list").html("");
$("#error-list").append(errorList);
$("#error-div").slideDown(500, function() {
$('#error-div').ScrollTo(300);
});
return false;
} else {
// Log the valid passing through this function in quote.shared.js:
logValidationErrors("N/A - Pre Quote", $("#prequote-form"), "productname", "none");
return true;
}
Ajax的功能:
// logValidationErrors
function logValidationErrors(xOrderNumber, xForm, xProduct, xErrors) {
/*
alert(xOrderNumber);
alert(xForm);
alert(xProduct);
alert(xErrors);
*/
$.ajax({
url: "log-clientside-events.php?" +
"type=javascript-errors&" +
"ordernumber=" + xOrderNumber + "&" +
"formvalues=" + encodeURIComponent(xForm.serialize()) + "&" +
"product=" + xProduct + "&" +
"errors=" + encodeURIComponent(xErrors),
context: document.body,
cache: false,
timeout: 10000,
success: function(sHTML){
},
error:function (xhr, ajaxOptions, thrownError){
//window.location.href = window.location.href;
}
});
}
阿努普幫助與解決方案。關鍵是創建一個在第一次運行Ajax時爲false的「標誌」變量,然後在ajax完成時將其設置爲true,然後再次提交表單,然後「標誌」檢查第二次跳過Ajax階段 - 並返回true。
if (errorList.length > 0) {
// Log the errors in quote.shared.js:
logValidationErrors("N/A - Customer Details", $("#customer-form"), "productname", errorList);
$("#error-list").html("");
$("#error-list").append(errorList);
$("#customer-login-success-message").slideUp(100);
$("#error-div").slideDown(500, function() {
$('#error-div').ScrollTo(300);
});
} else {
if (validationSuccessfullyLoggged) {
return true;
} else {
// Log the valid passing through this function in quote.shared.js:
logValidationErrors("N/A - Customer Details", $("#customer-form"), "productname", "none");
}
}
return false;
功能,與阿賈克斯涉及:
// logValidationErrors
var validationSuccessfullyLoggged = false;
function logValidationErrors(xOrderNumber, xForm, xProduct, xErrors) {
/*
alert(xOrderNumber);
alert(xForm);
alert(xProduct);
alert(xErrors);
*/
if (xErrors == "none") {
xErrors = "<li>Form Validated Successfully</li>";
submitForm = true;
} else {
submitForm = false;
}
$.ajax({
url: "log-clientside-events.php?" +
"type=javascript-errors&" +
"ordernumber=" + xOrderNumber + "&" +
"formvalues=" + encodeURIComponent(xForm.serialize()) + "&" +
"product=" + xProduct + "&" +
"errors=" + encodeURIComponent(xErrors),
context: document.body,
cache: false,
timeout: 10000,
success: function(sHTML){
//alert(validationSuccessfullyLoggged);
if (submitForm) {
validationSuccessfullyLoggged = true;
xForm.submit();
}
},
error:function (xhr, ajaxOptions, thrownError){
//window.location.href = window.location.href;
}
});
}
不應該使用形式POST請求,而不是對輸入數據提交到網址爲GET請求 –