如何访问动态表的最后一个单元格以获取总计

时间:2018-11-12 17:11:11

标签: javascript

我正在从最后一列输入的表中计算总数

当前td的警报显示[Object Nodelist]

function calcTotal() {
            var table = document.getElementById("invoice_details");
            //var tot = 0;
            var tot=$("#service_cost").val();
            //alert(tot);
            var sumvval=0;
            var table_length=table.rows.length;
            //alert(table_length);
            var td = document.querySelectorAll('#invoice_details > tbody > tr > td:last-child');
            //var val = document.querySelector('#invoice_details tbody tr td:last-child input').value;
            alert(td[0]);
            var total=0;
            for (var i = 0; i < td.length; i++)
            {
                total += parseFloat(td[i].innerText);
            }
            total=total+parseFloat(tot);

            document.getElementById("total_invoice_amount").value = parseFloat(total);

        }
<table class="table table-striped" id="invoice_details">
    <tbody id="generate_invoice_table">
    <tr>
    <td><input class="form-control" readonly="" value="57" name="item_id[]"></td>
    <td><input class="form-control" readonly="" value="Toyota" name="supplier_name[]"></td>
    <td><input hidden="" class="form-control" readonly="" value="Item" name="type[]"></td>
    <td><input class="form-control" readonly="" value="Fan Belt Aqua" name="item_description[]"></td>
    <td><input class="form-control" readonly="" value="1" name="available_qty[]"></td>
    <td><input class="form-control" readonly="" value="12000.00" name="retail_price[]"></td>
    <td><input type="text" value="0" class="form-control text-right" name="qty[]" onmousemove="calcTot(this.parentNode.parentNode.rowIndex)"></td>
    <td><input type="text" value="0" class="form-control text-right" name="discount[]" onmousemove="calcTot(this.parentNode.parentNode.rowIndex)"></td>
    <td><input type="text" class="form-control text-right" name="price[]"></td>
    </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

当您有一个数组并在表中使用时,您可以简单地从动态表中获取最后一个值。

var last = array.length -1 ;
console.log("Last value ::: ", array[last]);