1

I'm currently writing a small plugin for computer repair techs and I'm now trying to code the invoice section but not having much luck!

I've got the fields being populated from the database but I'm trying to use jquery to update the line totals and the total of the invoice.

Heres a quick image of what I'm trying to do!

enter image description here I'm trying to get it to update the totals on pageload and when a value changes in the unit cost and quantity textfields.

$(document).ready(function() {
    $("#invoiceitems tbody tr").each(function() {
        $(this).change(function() {
            updateTotal();
        });
    });
});

function updateTotal() {

    //Calculate Subtotal for each item
    var quantity = $('.quantity').val();
    var price = $('.price').val();
    var subtotal = parseInt(quantity) * parseFloat(price);
    $('.subtotal').text(subtotal);
    //Calculate Grand Total
    var sum = 0;
    $('.linetotal').each(function() {
        sum += parseFloat($(this).text());
    });
    $('#grandTotal').html(parseFloat(sum));
}​

Found this code on here but I really don't have a clue and can't get it to work!

Some direction/help would be greatly appreciated!

Thanks very much Jase

4

2 に答える 2

1

あなたは jQuery の完全な初心者のようです.. !! 良いスタートですが..それを拾うのは難しくありません..

ここで .price 、 .quantity 、および .lineTotal は、それらが話している列のクラス名に対応します. grandTotal は、テーブル内の Total の ID です..

私はあなたが取り組もうとしているケースのために更新された作業例を掲載 しました..良いスタートになるはずです..

ただし、ユーザー入力が有効かどうかを確認する条件を追加していないことに注意してください...ロールアウト中にそれらを追加してみてください..デコードで問題が発生した場合はお知らせください。

于 2012-09-07T21:10:49.897 に答える
1

例として、次の単純な HTML を考えます。

<table>
<tr>
<td>
<input type="text" name="unitCost[]" id="unitCost_1" value="15.99" class="editable"/>
</td>
<td>
<input type="text" name="quantity[]" id="quantity_1" value="1" class="editable"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="unitCost[]" id="unitCost_2" value="2.50" class="editable"/>
</td>
<td>
<input type="text" name="quantity[]" id="quantity_2" value="1" class="editable"/>
</td>
</tr>
</table>

jQuery を次のように変更する必要があると思います。

$(document).ready(function() {
    updateTotal();

    $(".editable").live("change", function() {
        updateTotal();
    });
});

updateTotal()実際の HTML を見ないと、機能に問題があるかどうかを判断するのは困難です

于 2012-09-07T20:59:14.160 に答える