Javascript 我的if和else语句是如何执行的?
这段代码运行得很好-但我的if和else语句都在执行,这是我以前从未见过的,也无法理解:Javascript 我的if和else语句是如何执行的?,javascript,jquery,ajax,Javascript,Jquery,Ajax,这段代码运行得很好-但我的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_' +
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);
}
事实上,在我的控制台中,所有行都首先通过
else
语句,因为变量未定义,然后再次通过常规if
语句,因为它们已定义,这让我感到困惑。如果我直接在ajax
行上方定义变量,那么变量怎么可能是未定义的呢?你确定color
和qty
在每次迭代中都有真实值吗?你能创建一个plunkr或fiddle吗?你能在你的ajax成功中添加一个console.log(),查看相对于错误调用了多少次。我不认为if和else是同时调用的。@BrianPowell很可能不是在同一时间触发的,而是在for
循环的不同迭代中触发的。使用console.log()
在每次迭代中观察这些变量的状态成功时,您只会增加completed\u行
,这意味着如果一行中有5行失败,它们都会返回相同的结果,因为“行”+completed\u行+“未成功。”
您没有得到失败的行,您只是得到了上一次成功的迭代+1
。
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}