2012-01-08 18 views
-2

テーブルセルの各テキストボックスの値を合計するのに問題があります。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** 

私はテキストボックスの値のそれぞれを合計することはできませんテキストボックス値の各セルを取得することができるけど、ここで私が書いたコードです。助けてください。これは緊急の問題です。ありがとう:)

+0

エラーは何ですか? 'total'を0に初期化しようとしましたか? 'cost'がローカル変数であることを確認しようとしましたか? –

答えて

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** 
+0

ありがとうございます。それは働く – user998405

関連する問題