function table_insert(lease_ids){
var lease_id=lease_ids+1;
var table = document.getElementById('table_data123'),
rows = table.getElementsByTagName('tr'),
i, j, cells, customerId;
for (i = 0, j = rows.length; i < j; ++i) {
cells = rows[i].getElementsByTagName('td');
if (!cells.length) {
continue;
}
sno = cells[0].innerHTML;
start_date = cells[1].innerHTML;
end_date = cells[2].innerHTML;
amount = cells[3].innerHTML;
$.ajax({
type: "post",
url: "insert_table_data.php",
data: {
sno: sno,
start_date: start_date,
end_date: end_date,
amount: amount,
lease_id: lease_id,
calendar_type: calendar_type
},
success: function(data){}
});
alert('success');
}
}
当我将数据发布到数据库时,总是会有多个警报框带有成功消息。当循环取10个值时,它将显示10个警告框。但我只需要一个警报框。
答案 0 :(得分:2)
我重写了一下你的代码,然后把ajax调用放到一个数组中。此数组传递给Promise.all()
,可用于在所有ajax调用完成后运行某些内容:
function table_insert(lease_ids) {
var ajaxCalls = [];
document.querySelectorAll('#table_data123 tr').forEach(row => {
var cells = row.getElementsByTagName('td');
if (cells.length) {
ajaxCalls.push(
$.post("insert_table_data.php", {
sno: cells[0].innerHTML,
start_date: cells[1].innerHTML,
end_date: cells[2].innerHTML,
amount: cells[3].innerHTML,
lease_id: lease_ids + 1,
calendar_type: calendar_type
}));
}
}); // end forEach()
Promise.all(ajaxCalls).then(() => {
alert("success");
});
}