从表输入中得到的总数是jquery中的总计?

时间:2016-07-05 15:58:29

标签: javascript jquery bootstrap-table

我为我的项目创建了一个简单的库存保存表。我还添加了一个按钮来向我的表添加行。这是我的表,

                            [add button]
+---------------+-----------+-----------+
+   lense type  +   qty     +   total   +
+---------------+-----------+-----------+
+               +           +           +
+---------------+-----------+-----------+
+               grand total : LKR       +
+---------------------------------------+

修改

我添加了表格的HTML代码,

<table class="table" id="tbl-add-lense">
            <thead style="background-color:#f5edff;">
            <th style="width:2%;"><input type="checkbox" name="chk_in" id="checkall"></input></th>
            <th style="width:2%;">item no</th>
            <th style="width:5%;">Lense Type</th>
            <th style="width:5%;">Unit price</th>
            <th style="width:5%;">Quantity</th>
            <th style="width:5%;">Total</th>
        </thead>
        <tbody id="tbl-lesne-body">
            <tr id="addr0">
                <td><input type="checkbox" name="chk_in"></input></td>
                <td>1</td> <td><input name='tb-lense-type1' type='text' placeholder='Lense Type' class='form-control '/> </td>
                <td><input  name='td-lunit1' type='number' placeholder='0'  class='form-control'></td>
                <td><input  name='td-lqty1' type='number' placeholder='0'  class='form-control'></td>
                <td><input  name='tb-ltotal1' type='number' placeholder='00.00'  class='form-control total'></td>

            </tr>

        </tbody>
        <tfooter></tfooter>
       </table>

此表有一行。我使用添加按钮添加更多行。添加行按钮代码,

$("#add-lense-row").click(function(){

      $("#tbl-lesne-body").append("<tr id='addr"+(i+1)+"'><td><input type='checkbox' name='chk_in'></input></td><td>"+ (i+1) +"</td> <td><input name='tb-lense-type"+i+"' type='text' placeholder='Lense Type' class='form-control '/> </td> <td><input  name='td-lunit"+i+"' type='number' placeholder='0'  class='form-control'></td><td><input  name='td-lqty"+i+"' type='number' placeholder='0'  class='form-control'></td><td class='tot'><input  name='td-ltotal"+i+"' type='number' placeholder='00.00' class='form-control total'></td></tr>");

      i++; 

   });

<td>有一个input

<input  name='tb-ltotal1' type='number' placeholder='00.00'  class='form-control total'>

输入输入时,我需要获得总td输入的总和。我试过这段代码,

 $(".total").keyup(function(){
            console.log('Typing');
            sum += parseFloat($(this).val()); 
        });

但它只适用于第一个表行。如果我添加一个新行并尝试此代码。它不起作用。我删除了sum += parseFloat($(this).val());行并尝试了。仍然只在第一行工作的代码。怎么解决这个问题。谢谢

1 个答案:

答案 0 :(得分:0)

您的代码需要进行一些更正:

  1. 您只获得触发'keyup'事件的输入值,作为总和。但是你需要使用类total遍历所有输入并添加all的值以获得总计。
  2. 由于只有第一行是通过html添加的,其余的行是通过javascript / jquery动态添加的,因此正常的事件绑定仅适用于第一行。对于动态生成的元素,即页面首次加载时不存在的元素,您需要使用稍微不同的语法进行事件绑定,例如$(document).on("keyup", ".total", function(){})。通过以这种方式动态绑定事件,keyup事件现在将触发所有输入。

    $(document).on("keyup", ".total", function(){
    
    console.log('Typing');
    var sum = 0;
    $(".total").each(function(index){
    sum += parseFloat($(this).val()); 
    });//each
    
    console.log( sum );
    
    });//keyup`