1

我有下面的代码,但是当它在语句中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>');

}
4

1 回答 1

0

我不确定typeof你的变量是什么,是它number还是只是一个string

iftypeof row.amount == "string"typeof myrowpaid.Amount == "string"thenif条件不会执行。

可以肯定的是,您的变量是number类型,使用parseInt()函数转换为数字

if (parseInt(row.amount, 10) > parseInt(myrowpaid.Amount, 10)){
                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>');
            }

如果您还没有对变量进行减速,请在使用 items 变量之前items添加var items = [];

于 2012-09-27T19:56:16.660 回答