2
我想提交一個POST到PHP腳本,而不使用表單。該功能除了實際的後部分以外都能正常工作。jQuery沒有提交POST表單
任何人都可以看到我在這裏做什麼一定是錯的?
function checkforRates(){
alert('activated');
//FUNCTION CHECKS FOR RATES FOR EACH OF THE ICPS IN LATEST CONTRACT
var count = $("#selectICP").children("option:not([disabled])").length;
success = 0
$('#selectICP option:not([disabled])').each(function() {
opICPs = $(this).val();
$.ajaxSetup({
type: 'POST',
URL: 'functions/workforce_newcontract.php',
data: {'checkrates': 'true', 'ICP': opICPs, 'ctype': ctype},
//data: '?checkrates=true&ICP=' + opICPs + '&ctype=' + ctype,
success: function(result) {
if(result == 1){
//THIS ICP HAS ALL METERS AND ENGINES WITH RATES
success = success + 1;
} else {
$('#contract_window_message_error_mes').html(result);
$('#contract_window_message_error').fadeIn(300).delay(3000).fadeOut(700);
return false;
}
},
error: function() {
$('#contract_window_message_error_mes').html("An error occured, the form was not submitted.");
$('#contract_window_message_error').fadeIn(300).delay(3000).fadeOut(700);
}
});
if(success === count){
//CONTINUE ONTO NEXT STAGE
alert('Success!');
}
});
}
非常感謝。
什麼不工作,在控制檯中是否有任何錯誤? – tymeJV
它看起來像你陷入了異步JavaScript的陷阱。我相信你所有的代碼都可以工作,但是你的if語句在ajax完成之前運行。您應該在您的ajax成功方法中放置一些console.log。如果你可以知道ajax沒有按預期工作,請包括debug console.log的輸出意味着什麼。 – TheThirdOne
$ .ajaxSetup不會執行AJAX調用,它只是爲未來的AJAX調用做準備。嘗試將其更改爲$ .ajax()並再次運行。此外,AJAX是異步的。我不認爲它可以在通話之後立即結束「if(success === count)」。 –