Jquery求和文本框值的每个单元格?

时间:2012-01-08 16:22:39

标签: jquery

我在表格单元格中对每个文本框值进行求和时遇到问题。这是我写的代码:

<asp:Table ID="table101" runat="server" CssClass="table100">
    <asp:TableRow>
        <asp:TableCell>Item 1 </asp:TableCell>
        <asp:TableCell>:</asp:TableCell>
        <asp:TableCell>
            <cus:cusTextBox ID="txtCDesc0" runat="server" Action="View"  ></cus:cusTextBox>
        </asp:TableCell>              
        <asp:TableCell>Cost: RM
            <cus:cusTextBox ID="txtCCost0" runat="server" Action="Edit" class="cost"  CssClass="inputTextM" OnKeyUp="return calculateTotalCost()"></cus:cusTextBox>                           
        </asp:TableCell>                   
    </asp:TableRow>

    <asp:TableRow>
        <asp:TableCell>Item 2</asp:TableCell>
        <asp:TableCell>:</asp:TableCell>
        <asp:TableCell>
            <cus:cusTextBox ID="txtCDesc1" runat="server" Action="View"  ></cus:cusTextBox>
        </asp:TableCell>              
        <asp:TableCell>Cost: RM
            <cus:cusTextBox ID="txtCCost1" runat="server" Action="Edit" class="cost"  CssClass="inputTextM" OnKeyUp="return calculateTotalCost()"></cus:cusTextBox>                           
        </asp:TableCell>                   
    </asp:TableRow>

    <asp:TableRow>
        <asp:TableCell>Item 3</asp:TableCell>
        <asp:TableCell>:</asp:TableCell>
        <asp:TableCell>
            <cus:cusTextBox ID="txtCDesc2" runat="server" Action="View"  ></cus:cusTextBox>
        </asp:TableCell>              
        <asp:TableCell>Cost: RM
            <cus:cusTextBox ID="txtCCost2" runat="server" Action="Edit" class="cost"  CssClass="inputTextM" OnKeyUp="return calculateTotalCost()"></cus:cusTextBox>                           
        </asp:TableCell>                   
    </asp:TableRow>

    <asp:TableRow>
        <asp:TableCell>Item 4 </asp:TableCell>
        <asp:TableCell>:</asp:TableCell>
        <asp:TableCell>
            <cus:cusTextBox ID="txtCDesc3" runat="server" Action="View"  ></cus:cusTextBox>
        </asp:TableCell>              
        <asp:TableCell>Cost: RM
            <cus:cusTextBox ID="txtCCost3" runat="server" Action="Edit" class="cost"  CssClass="inputTextM" OnKeyUp="return calculateTotalCost()"></cus:cusTextBox>                           
        </asp:TableCell>                   
    </asp:TableRow>

我的jQuery代码:

//--Calculate total cost--
function calculateTotalCost() {
    $('#<%=table101.ClientID %> tr').each(function () {
        var total;
        cost = parseFloat($(this).find("td").eq(3).find('input').val()).toFixed(2);
        total += cost;**//error**
        alert(total);
    });
}
//**Calculate total cost**

我能够获取文本框值的每个单元格,但无法计算每个文本框值。

1 个答案:

答案 0 :(得分:0)

您需要初始化循环外的总数。请尝试以下代码..

//--Calculate total cost--
function calculateTotalCost() {
    var total, cost;
    total = 0;
    $('#<%=table101.ClientID %> tr').each(function () {        
        cost = parseFloat($(this).find("td").eq(3).find('input').val()).toFixed(2);
        total += cost;        
    });
    alert(total);
}
//**Calculate total cost**