[英]How to have running total of asp:Labels using JavaScript in .aspx page
I am currently using this JavaScript to have a running total of textboxes that display to a asp:label. 我目前正在使用此JavaScript来运行显示到asp:label的文本框。
$(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));
});
});
});
How do I do the same for all the summed labels to sum themselves to a grand total of all the summed labels (to another label) ? 如何对所有求和的标签求和以将自己求和成所有求和的标签(对另一个标签)的总和?
per David's suggestion I have tried with the only result being the lblTotalsAll changed to "0". 根据David的建议,我尝试过的唯一结果是将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));
});
You'd start with the same concept: 您将从相同的概念开始:
var total = 0;
$(".myLabel0").each(function () {
// add to the total
});
// do something with the total
Now, since an asp:Label
generates a <span>
and not an <input>
, you can't get the value from .value
. 现在,由于
asp:Label
生成<span>
而不是<input>
,因此您无法从.value
获取值。 Instead, you'd need to parse it from the text that you're currently writing to the element. 相反,您需要从当前正在写入元素的文本中对其进行解析。 Something like this:
像这样:
var labelValue = $(this).text();
if (!isNaN(labelValue) && labelValue.length != 0) {
total += parseFloat(labelValue);
}
Structurally you're basically doing what you already do for the values of text boxes, but for the contents of <span>
elements instead. 从结构上讲,您基本上是在对文本框的值进行操作,但是对
<span>
元素的内容进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.