gpt4 book ai didi

JavaScript 计算未按预期工作

转载 作者:行者123 更新时间:2023-12-03 05:02:34 25 4
gpt4 key购买 nike

我有一个 gridview,其中有一行添加了整数值,例如 7000

因此,如果添加了 2-3 行,我想添加所有这些行值并将其显示在文本框

为此,我编写了以下代码

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

加法操作不起作用。

最佳答案

我将你的问题解释为你有一个包含行值的表,你想要对每一行求和,然后你想要获得整个表的行总和。

我想提供一个有效的代码片段来演示您的问题的解决方案。但是,由于您没有提供所有必要的代码,所以我发明了其中一些代码。在此过程中,我提供了一种替代的、也许更现代的方法来解决我认为您试图解决的总体问题。是的,它提供的内容远远超过您所要求的内容(...我认为讨论 parseInt 的其他一些答案可能会解决您最初的问题),但希望这能给您带来一些进一步的见解。

下面的示例展示了如何使用 JavaScript 的许多最新功能来执行此操作。希望这些评论能够足够详细地解释每一步发生的情况,以便您了解其中的逻辑。要理解每个步骤,您可能需要更深入地学习更多 JavaScript。一个很好的引用是 Mozilla Developer Network (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>

关于JavaScript 计算未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42159541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com