如何使用 JavaScript 计算 GridView 列的总和?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6288504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:11:14  来源:igfitidea点击:

How to Calculate the Sum of GridView Columns Using JavaScript?

javascriptasp.netuser-controls

提问by thevan

I have a GridView with three COlumns Which will be like this:

我有一个带有三个列的 GridView,如下所示:

 ID                  Sign                Amount
------              --------             ---------
 1                    +                   1000
 2                    -                    500
 3                    -                    750

So the Sum of the Column "Amount" Should be "-250". Consider the Column "Sign" also with the Amount. Here is my GridView's Source Code:

所以列“金额”的总和应该是“-250”。还要考虑带有金额的“签名”列。这是我的 GridView 的源代码:

 <Columns> 
  <asp:BoundField DataField="ID" HeaderText="ID"/>
       <asp:TemplateField HeaderText="Sign" >
        <ItemTemplate>
 <asp:TextBox ID="txtgvSign" runat="server" Text='<%# Bind("Sign") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
 <asp:TemplateField HeaderText="Amount">
  <ItemTemplate>
 <asp:TextBox ID="txtAmount" runat="server" Text='<%# Bind("Amount") %>'></asp:TextBox>  
</ItemTemplate>
</asp:TemplateField>
</Columns> 

So far, I have written One JavaScript, But It Only Calculate the Sum of Amount. I dont know how to calculate the Sum based on the Sign values. Below is the JavaScript I have written:

到目前为止,我已经写了一个 JavaScript,但它只计算金额的总和。我不知道如何根据符号值计算总和。下面是我写的 JavaScript:

  function CalculateTax(fixedtotal)
  {
    var taxgrid = document.getElementById('<%=gvAttribute.ClientID %>');
    var taxip = taxgrid.getElementsByTagName('input');
    var taxamount = 0*1;

    for(i = 0; i < taxip.length; i++)
    {
        var tax = taxip[i].value;            
        taxamount = parseFloat(taxamount) + parseFloat(tax);
    }
    return parseFloat(fixedtotal) + parseFloat(taxamount);
  }

So Please Make Changes to this Javascript.

所以请对这个 Javascript 进行更改。

回答by Akhil

can you try the below code?

你可以试试下面的代码吗?

  function CalculateTax(fixedtotal)
  {
    var taxgrid = document.getElementById('<%=gvAttribute.ClientID %>');
    var taxip = taxgrid.getElementsByTagName('input');
    var taxamount = 0*1;

    for(i = 0; i < taxip.length; i+= 2)
    {
        var sign = taxip[i].value;            
        var tax = taxip[i+1].value; 
        taxamount = parseFloat(taxamount) + (sign =='+' ? 1 : -1)* parseFloat(tax);
    }
    return parseFloat(fixedtotal) + parseFloat(taxamount);
  }

回答by Anuradha Jayalath

<script language="javascript" type="text/javascript">
    function Calculate() {

        var grid = document.getElementById("<%=grid.ClientID%>");
        var sum = 0;
        for (var i = 1; i < grid.rows.length; i++) {
            var Cell = grid.rows[i].getElementsByTagName("input");
            if (!Cell[4].value) {sum += 0; } else { sum += parseFloat(Cell[0].value);}
        }

        alert(sum);        
    }

</script> 



<asp:TemplateField HeaderText="Current payment" >
<ItemTemplate>
    <asp:TextBox ID="txtvalue" runat="server" Width="70px" BorderStyle="None" onkeyup="Calculate();" ></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="120px" />
</asp:TemplateField>`enter code here`