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あなたの変数が何であるかはよくわかりませnumberstring

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>');
            }

変数をまだ decelear していない場合は、items 変数を使用する前itemsに追加しますvar items = [];

于 2012-09-27T19:56:16.660 に答える