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)”。 –