我有一个gridview,其中有一个行,其中我添加了整数值,比如7000。
因此,如果添加了2-3行,我希望添加所有这些行值,并在textbox中显示它。
为此,我编写了以下代码
if (document.getElementById('txtTotalExp').value == "") {
var Expense = 0;
} else {
var Expense = document.getElementById('txtTotalExp').value;
}
for (var i = 0; i < GridExpInfo.Rows.length; i++) {
var totAmount = GridExpInfo.Rows[i].Cells[7].Value;
var totval = totAmount;
Expense += parseInt(totval);
}document.getElementById('txtTotalLandVal').value = parseInt(TotalPayableValue) + parseInt(Expense);
但在文本框中,出现了类似于200010001512的内容。
加法操作不工作。
发布于 2017-02-10 14:48:19
我把你的问题解释为你有一个有几行值的表,你想要和每一行,然后你想得到整个表的总和。
我想提供一个工作代码片段来演示您的问题的解决方案。但是,因为您没有提供所有必要的代码,所以我发明了一些代码。在这样做的过程中,我提供了一种替代的,也许更现代的方法来解决我认为你正在试图解决的整体问题。是的,它提供的比您所要求的要多得多(...I认为讨论parseInt的其他一些答案可能解决您最初的问题),但希望这能给您提供一些更深入的见解。
下面的示例展示了如何使用JavaScript的许多最新特性来实现这一点。希望这些评论能够详细地解释每一步发生的事情,让你对逻辑有一种感觉。要理解每一步,您可能需要深入学习更多的JavaScript。一个很好的参考是Mozilla开发者网络(MDN),它包含关于所有这些特性的文档。
// when the button is clicked, do the following...
document.querySelector('button').onclick = () => {
// calculate the total sum across the table
const rowSums =
// get a nodeList of all table rows in the entire document
// and convert that array-like nodeList to a true array
// of table row elements
[...document.querySelectorAll('tr')]
// remove the first row, i.e. ignore the row of column headers
.slice(1)
// from the original array of table rows (minus the first row)
// create a new array in which each element is derived
// from the corresponding table row from the original array
.map(row => {
// calculate the sum of values across this row
const rowSum =
// get a nodeList of all table cells in this row
// and convert that array-like nodeList to a true array
// of table cell elements
[...row.querySelectorAll('td')]
// remove the last cell, i.e. ignore the cell that will eventually
// hold the sum for this row
.slice(0,-1)
// from the array of table cells for this row (minus the last one)
// derive a new single value by progressively doing something
// for each cell
.reduce(
// for each table cell in this row, remember the cell itself
// as well as the accumulating value we are gradually deriving from all
// the cells in this row, i.e. the sum of all values across this row
(accumulatingRowSum, cell) =>
// for each cell in this row, add the numerical value
// of the current cell to the sum of values we are accumulating
// across this row
accumulatingRowSum + parseInt(cell.innerHTML, 10),
// start our accumulating sum of values across this row with zero
0
);
// get all the cells in this row
const rowCells = row.querySelectorAll('td');
// put the sum of values from this row into the last cell of the row
rowCells[rowCells.length - 1].innerHTML = rowSum;
// place the sum of values in this row into the accumulating array
// of all such values for all rows
return rowSum;
});
// calculate the total sum for the whole table
const totalExpenses =
// start with the array of sums for each row
rowSums
// similar to `reduce` above, reduce the array of multiple row sums
// into a single value of the total sum, calculated by adding together
// all the individual row sums, starting with zero
.reduce((accumulatingTotalSum, rowTotal) => accumulatingTotalSum + rowTotal, 0);
// place the total sum into the appropriate span element
document.querySelector('#txtTotalExp').innerHTML = totalExpenses;
};table {
border: solid black 1px;
border-collapse: collapse;
}
th, td {
border: solid black 1px;
padding: 0.5em;
}<table>
<tr><th>col1</th><th>col2</th><th>row total</th></tr>
<tr><td>3500</td><td>1200</td><td></td></tr>
<tr><td>2700</td><td>4500</td><td></td></tr>
<tr><td>3100</td><td>1300</td><td></td></tr>
</table>
<p>The total expenses are: <span id="txtTotalExp"></span></p>
<button>Calculate</button>
发布于 2017-02-10 13:03:11
您可以使用以下代码:
var total=parseInt(TotalPayableValue,10) + parseInt(Expense,10);
document.getElementById('txtTotalLandVal').value=total;发布于 2017-02-10 13:51:32
试试这个:
var Expense = 0;
if (document.getElementById('txtTotalExp').value != "") {
var Expense = parseInt(document.getElementById('txtTotalExp').value);
}
for (var i = 0; i < GridExpInfo.Rows.length; i++) {
var totAmount = GridExpInfo.Rows[i].Cells[7].Value;
Expense += parseInt(totAmount);
}问题是:最初的开销可以是字符串。您是否尝试过如果使用“费用= 0”,就会发生错误?
https://stackoverflow.com/questions/42159541
复制相似问题