繁体   English   中英

JavaScript计算无法正常工作

[英]Javascript calculation is not working as expected

我有一个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的内容

加法操作不起作用。

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

我想提供一个工作代码段来演示您的问题的解决方案。 但是,由于您没有提供所有必要的代码,因此我发明了其中的一些代码。 在这样做的过程中,我提供了一种替代的方法,也许是更现代的方法来解决我认为您要解决的整体问题。 是的,它提供的功能远远超出您的要求(...我认为讨论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, ie 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, ie 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, ie 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> 

您可以使用以下代码:

var total=parseInt(TotalPayableValue,10) + parseInt(Expense,10);
    document.getElementById('txtTotalLandVal').value=total;

尝试这个:

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”发生错误?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM