簡體   English   中英

僅計算相關行的總和

[英]Calculating sum only for relevant row

所以我有幾個小時無法解決的問題..

當我按下按鈕“Pridėtiprekę”時,一行會動態添加到表中,當我在“ Kiekis”和“ Kaina”列中寫數字時,“ Suma”列中什么也不會發生(僅發生在動態添加的行中)。

一塊jQuery:

$(document).on('keyup', '#quantity, #price', function() {
    $('#total').val(($('#quantity').val() * $('#price').val()).toFixed(2));
});

請查看JSFiddle,它如何“工作”我的代碼:

https://jsfiddle.net/w5qz5exe/7/

預先感謝您的幫助!

PS我試圖將div ID total更改為div class total,然后起作用了,但是它適用於所有行,而不適用於相關行。

您正在引用帶有ID的所有內容。

    $(document).on('keyup', '.quantity, .price', function() {
        var row = $(this).closest('tr');
        var rowPrice = $(row).find('.price').val();
        var rowQuantity = $(row).find('.quantity').val();

        $(row).find('.total').val( (rowPrice * rowQuantity).toFixed(2) )
    });

https://jsfiddle.net/w5qz5exe/12/

您的問題是輸入正在使用id代替其他東西。 查找有問題的項目時,搜索將在找到的第一個ID處停止。

這個更新的小提琴https://jsfiddle.net/w5qz5exe/11/顯示了如何使用類來完成。

        $(document).on('keyup', '.quantity, .price', function(e) {
            var $parent = $(e.target).parents('tr'),
                $total = $parent.find('.total'),
                $quantity = $parent.find('.quantity'),
                $price = $parent.find('.price');
            $total.val(($quantity.val() * $price.val()).toFixed(2));
        });

除上述內容外,從輸入中刪除所有Id,並將其更改為類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM