这段代码运行得很好 - 但我的if和else语句都在执行,这是我以前从未见过的,无法理解的:
function submit(total_lines) {
total_lines = total_lines;
completed_lines = 0;
/* create one line at a time... */
for (i=0;i<total_lines;i++) {
color = $('#color_' + i).val();
qty = $('#qty_' + i).val();
console.log("starting line " + completed_lines);
if ( color && qty ) {
$.ajax({
type: 'POST',
url: '/submit_line.php',
data: { color:color, qty:qty },
success:function(data){
console.log("Line " + completed_lines + " was successfull");
completed_lines++;
}
}); //close ajax
} else {
console.log("Line " + completed_lines + " was not successfull. color = " + color + " and qty = " + qty);
toastr.error("Color and quantity are both required fields.");
}
}
setTimeout(function(){
toastr.success("All done! ...");
$('#add_something_footer').html('<div class="footer_buttons_holder"><div onclick="close_swal()" class="footer_cancel_button">CLOSE</div></div>');
}, 5000);
}
对于i
的每次迭代,submit_line.php
正在执行,但我也在我的toastr.error
语句中执行else
行。这怎么可能 - color
和qty
都存在,所以if
语句的主要部分正在执行..我不相信我的语法在任何地方都很糟糕,否则什么都不会执行...)。我很困惑......
更新了OP以引用console.log语句。
这是我的console.log
Object {type: "info", iconClass: "toast-info", message: "Submitting quotes...", optionsOverride: undefined, title: undefined}
5VM7093:699 starting line 0
VM7093:712 Line 0 was not successfull. color = undefined and quantity = undefined
toastr_qt.js:227 Object {type: "error", iconClass: "toast-error", message: "Color and quantity are both required fields", optionsOverride: undefined, title: undefined}
VM7093:699 starting line 0
VM7093:712 Line 0 was not successfull. color = undefined and quantity = undefined
toastr_qt.js:227 Object {type: "error", iconClass: "toast-error", message: "Color and quantity are both required fields", optionsOverride: undefined, title: undefined}
VM7093:699 starting line 0
VM7093:712 Line 0 was not successfull. color = undefined and quantity = undefined
toastr_qt.js:227 Object {type: "error", iconClass: "toast-error", message: "Color and quantity are both required fields", optionsOverride: undefined, title: undefined}
VM7093:699 starting line 0
VM7093:712 Line 0 was not successfull. color = undefined and quantity = undefined
toastr_qt.js:227 Object {type: "error", iconClass: "toast-error", message: "Color and quantity are both required fields.", optionsOverride: undefined, title: undefined}
VM7093:699 starting line 0
VM7093:712 Line 0 was not successfull. color = undefined and quantity = undefined
toastr_qt.js:227 Object {type: "error", iconClass: "toast-error", message: "Color and quantity are both required fields.", optionsOverride: undefined, title: undefined}
VM7093:708 Line 1 was successfull
VM7093:708 Line 2 was successfull
VM7093:708 Line 3 was successfull
VM7093:708 Line 4 was successfull
toastr_qt.js:227 Object {type: "success", iconClass: "toast-success", message: "All done!", optionsOverride: undefined, title: undefined}
事实上,在我的控制台中 - 所有行都通过else
语句FIRST,因为变量未定义,然后再次通过常规if
语句,因为它们被定义是这样的事情令我困惑。如果我在undefined
行的正上方定义变量,该变量如何为ajax
?