Javascript计算无法按预期工作

时间:2017-02-10 12:34:31

标签: javascript asp.net gridview

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

添加操作无效。

3 个答案:

答案 0 :(得分:1)

我将您的问题解释为表示您有一个包含值行的表,您希望对每一行求和,然后您希望获得整个表的总行数总和。

我想提供一个有效的代码段来演示您的问题的解决方案。但是,因为你没有提供所有必要的代码,所以我发明了一些代码。在这样做的过程中,我提供了一种替代的,也许是更现代的方法来解决我认为你试图解决的整体问题。是的,它提供的内容比您要求的要多得多(...我认为讨论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>

答案 1 :(得分:0)

您可以使用以下代码:

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

答案 2 :(得分:0)

试试这个:

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);
}

问题:费用最初可以是一个字符串。您是否尝试使用“Expense = 0”发生错误?