I am currently using this JavaScript to have a running total of textboxes that display to a asp:label.
我目前正在使用这个JavaScript来运行总共显示为asp:标签的文本框。
$(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));
});
1 个解决方案
#1
0
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
. Instead, you'd need to parse it from the text that you're currently writing to the element. Something like this:
现在,由于asp:Label生成而不是,因此无法从.value获取值。相反,您需要从当前正在写入元素的文本中解析它。像这样的东西:
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.
从结构上讲,你基本上是在做你已经为文本框的值做的事情,而是替代元素的内容。
#1
0
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
. Instead, you'd need to parse it from the text that you're currently writing to the element. Something like this:
现在,由于asp:Label生成而不是,因此无法从.value获取值。相反,您需要从当前正在写入元素的文本中解析它。像这样的东西:
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.
从结构上讲,你基本上是在做你已经为文本框的值做的事情,而是替代元素的内容。