简体   繁体   English

使用动态添加的行以表格形式计算行总计

[英]Calculating row totals in form using dynamically added rows

I'm creating a form to input an invoice with dynamically added rows ( as seen in this image ) 我正在创建一个表单以输入带有动态添加的行的发票( 如该图所示

I'm using jquery 1.6.2 to calculate Line Total, sum all Line Totals, calculate Tax and display Total. 我正在使用jquery 1.6.2计算行总数,对所有行总数求和,计算税收并显示总数。 The problem that I'm having is that calculating Line Total only works for the first added row. 我遇到的问题是,计算行总数仅适用于添加的第一行。 As you can see on the image above, the first row (Item 1), is fine (2*30=60), but the second row (Item 2) shows the same total as the first one (it should be 4*100=400). 如上图所示,第一行(项目1)很好(2 * 30 = 60),但是第二行(项目2)显示的总金额与第一行相同(应为4 * 100) = 400)。 Here's the code for this part: 这是此部分的代码:

<script type="text/javascript">
var $k = jQuery.noConflict();

$k(function (){
    $k('#item_form').live("keyup", "input[name^=linetotal]", function() {
        var quantity = $k('input[name^=quantity]').val();
        var unitprice = $k('input[name^=unitprice]').val();
        $k('input[name^=linetotal]').val(quantity*unitprice);

        var sum = $k('input[name^=linetotal]').sum();

        $k('#subtotal').val(sum);
        $k('#tax').val(Math.round(sum*10*100)/100);
        $k('#total').val(Math.round((sum+Math.round(sum*10*100)/100)*100)/100);
     });
});
</script>   

Any ideas about what should I try? 关于我应该尝试什么的任何想法? Thank you all! 谢谢你们!

You are using $k(input[name^=quantity]').val(); 您正在使用$k(input[name^=quantity]').val(); to grab the quantity/unitprice, but this grabs all quantity/unitprice inputs, and .val() gets the value of the first one. 捕获数量/单价,但这会捕获所有数量/ .val()输入, .val()获取第一个的值。

You need to get the one on the same line, although it is odd that you are waiting for keyup on the linetotal . 您需要在同一行上获得一个,尽管奇怪的是您正在等待linetotal上的linetotal Wouldn't you want keyup on quantity and unitprice ? 难道你不希望KEYUP的quantityunitprice

 $k('input[name^=quantity], input[name^=unitprice]').live("keyup", function() {
        var quantity = parseFloat($k(this).parent().find('input[name^=quantity]').val());
        var unitprice = parseFloat($k(this).parent().find('input[name^=unitprice]').val());
        $k(this).siblings('input[name^=linetotal]').val(quantity*unitprice);

        var sum = $k(this).siblings('input[name^=linetotal]').sum();

        $k('#subtotal').val(sum);
        $k('#tax').val(Math.round(sum*10*100)/100);
        $k('#total').val(Math.round((sum+Math.round(sum*10*100)/100)*100)/100);
     });
});

Basic jsfiddle demo: http://jsfiddle.net/jtbowden/YxJCF/ 基本jsfiddle演示: http : //jsfiddle.net/jtbowden/YxJCF/

I believe this is due it taking the first input[name^quantity] and first input[name^unitprice] it runs across. 我相信这是由于它遇到了第一个输入[name ^ quantity]和第一个输入[name ^ unitprice]。 Since both rows have the same names, you have to scope your code to make sure it only pulls from its own row and not from others. 由于两行名称相同,因此您必须确定代码范围,以确保其仅从自己的行中提取,而不从其他行中提取。 Does that make sense? 那有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM