2016-08-15 3 views
0

現在、このJavaScriptを使用して、asp:ラベルに表示されているテキストボックスを合計しています。asp:aspxページでJavaScriptを使用しているラベル

$(document).ready(function() { 
    //Iterate through each Textbox and add keyup event handler 
    $(".myTextBox0").each(function() { 
     $(this).keyup(function() { 
      //Initialize total to 0 
      var total = 0; 
      $(".myTextBox0").each(function() { 
       // Sum only if the text entered is number and greater than 0        
       if (!isNaN(this.value) && this.value.length != 0) { total += parseFloat(this.value); } 
      }); 
      //Assign the total to 
      //.toFixed() method will roundoff the final sum to 2 decimal places 
      $('#<%=lblJobTotals0.ClientID %>').html(total.toFixed(2)); 
     }); 
    }); 
}); 

私は(別のラベルへの)すべての合計ラベルの総計に身を合計するすべての合計ラベルに同じことをどのように行うのですか?

私は唯一の結果として、lblTotalsAllが "0"に変更されたことを試しました。

$(document).ready(function() { 
    //Iterate through each Labels 
    var total = 0; 
    // all labels to sum have cssClass = .lblTotals 
    $(".lblTotals").each(function() { 
     // add to the total 
      var labelValue = $(this).text(); 
     if (!isNaN(labelValue) && labelValue.length != 0) { 
      total += parseFloat(labelValue); 
     } 

}); 

$('#<%=lblJobTotalsAll.ClientID %>').html(total.toFixed(2)); 

}); 

答えて

0

あなたは同じコンセプトで始めたい:asp:Label<span>なく<input>を生成するので、あなたは.valueから値を取得することはできません、

var total = 0; 
$(".myLabel0").each(function() { 
    // add to the total 
}); 
// do something with the total 

。代わりに、現在要素に書き込んでいるテキストから解析する必要があります。このような何か:

var labelValue = $(this).text(); 
if (!isNaN(labelValue) && labelValue.length != 0) { 
    total += parseFloat(labelValue); 
} 

構造的にあなたは基本的にすでにテキストボックスの値のため、代わりに<span>要素の内容のために何をすべきかやっています。

+0

help @Davidに感謝します。私は成功なしで次のことを試みた。 { //各ラベルを繰り返し処理する var total = 0; //すべてのラベルにはcssClass = .lblTotalsがあります $( ".lblTotals")。each(function(){ 総 VAR labelValue = $(この)は.text()に追加//; をするif(!!ますisNaN(labelValue)&& labelValue.length = 0){ 合計+ = parseFloatは(labelValue);} } ); $( '#<%= lblJobTotalsAll.ClientID%>').html(total.toFixed(2)); }); – AhabLives

+0

@AhabLives:コメントのコードはほとんど読めない。また、「成功しません」と定義します。どのように失敗しましたか? – David

+0

私は元の質問を更新しました。希望が役立ちます。 - ありがとう。 – AhabLives

関連する問題