我有下面的代碼,但items.push
不在if
語句中工作。如果在結束}
之前取消註釋該行,則items.push
按預期工作。推送不起作用,如果語句
for (i = 0; i < len; i += 1) {
row = resultexpense.rows.item(i);
t.executeSql('SELECT * FROM expensepayments WHERE Barcode = ?',
[row.barcode],
function(t, resultpaid) {
var myrowpaid,
myrowpaidlen;
myrowpaidlen = resultpaid.rows.length;
alert(myrowpaidlen); //alerts 1
if (myrowpaidlen > 0){
myrowpaid = resultpaid.rows.item(0);
alert(row.amount); //alerts 90
alert(myrowpaid.Amount); //alerts 50
if (row.amount > myrowpaid.Amount){
alert(row.amount- myrowpaid.Amount); //alerts 40
items.push('<li><a href="#displayexpense" data-description="' + row.description + '" data-buildingcode = "' + row.buildingcode + '" data-barcode="' + row.barcode + '" data-amount="' + row.amount + '" data-buildingaddress="' + row.buildingaddress + '">' + row.description + '</a></li>');
}
} else {
items.push('<li><a href="#displayexpense" data-description="' + row.description + '" data-buildingcode = "' + row.buildingcode + '" data-barcode="' + row.barcode + '" data-amount="' + row.amount + '" data-buildingaddress="' + row.buildingaddress + '">' + row.description + '</a></li>');
}
});
// items.push('<li><a href="#displayexpense" data-description="' + row.description + '" data-buildingcode = "' + row.buildingcode + '" data-barcode="' + row.barcode + '" data-amount="' + row.amount + '" data-buildingaddress="' + row.buildingaddress + '">' + row.description + '</a></li>');
}
'executeSql()'調用是**異步**。回調中的代碼在數據庫響應之後纔會運行,在整個循環結束後將會*。 – Pointy
另外,你可能應該編輯你之前的問題,因爲它是一樣的。 – Pointy
可能重複的[錯誤在JavaScript循環(推到數組不工作)](http://stackoverflow.com/questions/12628321/error-in-javascript-loop-push-to-array-does-not-work ) – Pointy