简体   繁体   English

使用jQuery计算GridView中的行总数和总计

[英]Calculate Row Total and Grand Total in gridview using jquery

Hi in below JavaScript code the multiplication is between txtQuantity which is textbox TemplateField in grid view and price which is BoundField in grid view. 嗨,在下面的JavaScript代码中,乘法运算是在txtQuantity即网格视图中的文本框TemplateField)和price(即网格视图中的BoundField)之间进行的。

My issue is my grid view have both txtQuantity and txtPrice as TemplateFieldes 我的问题是我的网格视图同时具有txtQuantity和txtPrice作为TemplateFieldes

<ItemTemplate><asp:TextBox ID="txtprice" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox></ItemTemplate>

<ItemTemplate><asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox></ItemTemplate>

i have tried to modify the java-script code 我试图修改Java脚本代码

 $("[id*=lblTotal]", row).html(parseFloat($("[id*=txtprice]", row).html()) * parseFloat($(this).val()));

but it give me NaN as result of multiplication 但是由于乘法,它给了我NaN

Original js code 原始js代码

 <script type="text/javascript"> $(function () { $("[id*=txtQuantity]").val("0"); }); $("[id*=txtQuantity]").live("change", function () { if (isNaN(parseInt($(this).val()))) { $(this).val('0'); } else { $(this).val(parseInt($(this).val()).toString()); } }); $("[id*=txtQuantity]").live("keyup", function () { if (!jQuery.trim($(this).val()) == '') { if (!isNaN(parseFloat($(this).val()))) { var row = $(this).closest("tr"); $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val())); } } else { $(this).val(''); } var grandTotal = 0; $("[id*=lblTotal]").each(function () { grandTotal = grandTotal + parseFloat($(this).html()); }); $("[id*=lblGrandTotal]").html(grandTotal.toString()); }); </script> 

There are some minor issues with your code, try this: 您的代码存在一些小问题,请尝试以下操作:

 $(function () { $("[id*=txtQuantity").val("0"); }); $(document).on("change", "[id*=txtQuantity]", function () { if (isNaN(parseInt($(this).val()))) { $(this).val('0'); } else { $(this).val(parseInt($(this).val()).toString()); } }); $(document).on("keyup mouseup", "[id*=txtQuantity]", function () { if (!jQuery.trim($(this).val()) == '') { if (!isNaN(parseFloat($(this).val()))) { var row = $(this).closest("tr"); $("[id*=lblTotal]", row).html(parseFloat($("[id*=txtPrice]", row).val()) * parseFloat($(this).val())); } } else { $(this).val(''); } var grandTotal = 0; $("[id*=lblTotal]").each(function () { var value = $(this).html(); if(value != "") grandTotal = grandTotal + parseFloat(value); }); $("[id*=lblGrandTotal]").html(grandTotal.toString()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table cellspacing="5"> <tr> <td><input type="text" id="txtPrice1" value="100" /></td> <td><input type="text" id="txtQuantity1" value="1" /></td> <td><div id="lblTotal1" /></td> </tr> <tr> <td><input type="text" id="txtPrice2" value="200" /></td> <td><input type="text" id="txtQuantity2" value="2" /></td> <td><div id="lblTotal1" /></td> </tr> <tr> <td><input type="text" id="txtPrice3" value="300" /></td> <td><input type="text" id="txtQuantity3" value="3" /></td> <td><div id="lblTotal1" /></td> </tr> </table> <div id="lblGrandTotal" /> 

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

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