如何将从具有两个单独对象的数组列表生成的表列中的所有值相加



我想在我的表格中总计一列。我的数组列表包含两个对象,并使用该数组中的函数生成一个表。我对一个特定列(英里数(求和的函数没有向我的网页输出任何东西。我希望在单击"获取状态"时,显示要显示的总里程数。

我尝试了将提到的代码放在单独的函数中,但它不起作用。

<body>
   <label>Please enter your flight Number:</label><br>
   <input type="text" id="flightNumber" name="flightnumber" value=""  /> <br />
   <label>Please enter Miles Flown:</label><br>
   <input type="text" id="milesFlown" name="milesflown" value="" />
 <br>
   <input type="button" id="display" name="display" value="Submit Flight Information" />
   <br>
   <div id ="classMessages"></div>
   <input type="button" id="status" name="status" value="Get Class Status" onclick= getClassStatus(); />
   <table id="flightTable">
       <tr>
         <th>Flight Number</th>
         <th>Miles Flown</th>
       </tr>
     </table>
     </div>


     <script type="text/javascript">
  const flightTable = document.getElementById('flightTable'),
  button = document.getElementById('display'),
  flightNum = document.getElementById('flightNumber'),
  milesFlown = document.getElementById('milesFlown'),
  addRow = () => {
    const tr = document.createElement('tr'),
      tdFlightNo = document.createElement('td'),
      tdMilesFlown = document.createElement('td');
      tdMilesFlown.setAttribute('class', 'needsToBeCounted');
    /** getting the last record in the flight objects array **/
    tdFlightNo.textContent = flightArray[i - 1].flightNumber;
    tdMilesFlown.textContent = flightArray[i - 1].milesFlown;
    /** append the TDs elements to the TR element (all of them are created above dynamically) **/
    tr.append(tdFlightNo, tdMilesFlown);
    /** append that row to the HTML table **/
    flightTable.appendChild(tr);
  }
let flightArray = [],
  flightNumValue = null,
  milesFlownValue = null,
  i = 0;
button.addEventListener('click', () => {

  flightNumValue = flightNum.value;
  milesFlownValue = milesFlown.value;
  /** checking for duplicate entry **/
  if (flightArray.find(el => {
      return el.flightNumber === flightNumValue
    })) {
    alert('You cannot enter this flight due to Duplicate Flight Number entry: "' + flightNumValue + '"');
    return false;
  }

  /** add the entry in the flight objects table **/
  flightArray[i++] = {
    flightNumber: flightNumValue,
    milesFlown: milesFlownValue
  }; /** add the flight record to the array and increment the counter i (notice the i++) **/
  addRow(); /** call addRow to add a new row in the table (HTML) **/

});

function getClassStatus(){
  var cls = document.getElementById("flightTable").getElementsByTagName("td");
  for (var i = 0; i < cls.length; i++){
    if(cls[i].className == "needsToBeCounted"){
        total += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
    }
    console.log(total);
    </script>

Uncaught ReferenceError: getClassStatus is not defined单击我的按钮以总计里程时。

代码只有一个问题,total变量没有声明。

var total = 0;
const flightTable = document.getElementById('flightTable'),
  button = document.getElementById('display'),
  flightNum = document.getElementById('flightNumber'),
  milesFlown = document.getElementById('milesFlown'),
  addRow = () => {
    const tr = document.createElement('tr'),
      tdFlightNo = document.createElement('td'),
      tdMilesFlown = document.createElement('td');
    tdMilesFlown.setAttribute('class', 'needsToBeCounted');
    /** getting the last record in the flight objects array **/
    tdFlightNo.textContent = flightArray[i - 1].flightNumber;
    tdMilesFlown.textContent = flightArray[i - 1].milesFlown;
    /** append the TDs elements to the TR element (all of them are created above dynamically) **/
    tr.append(tdFlightNo, tdMilesFlown);
    /** append that row to the HTML table **/
    flightTable.appendChild(tr);
  }
let flightArray = [],
  flightNumValue = null,
  milesFlownValue = null,
  i = 0;
button.addEventListener('click', () => {
  flightNumValue = flightNum.value;
  milesFlownValue = milesFlown.value;
  /** checking for duplicate entry **/
  if (flightArray.find(el => {
      return el.flightNumber === flightNumValue
    })) {
    alert('You cannot enter this flight due to Duplicate Flight Number entry: "' + flightNumValue + '"');
    return false;
  }
  /** add the entry in the flight objects table **/
  flightArray[i++] = {
    flightNumber: flightNumValue,
    milesFlown: milesFlownValue
  }; /** add the flight record to the array and increment the counter i (notice the i++) **/
  addRow(); /** call addRow to add a new row in the table (HTML) **/
});
function getClassStatus() {
  var cls = document.getElementById("flightTable").getElementsByTagName("td");
  for (var i = 0; i < cls.length; i++) {
    if (cls[i].className == "needsToBeCounted") {
      total += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
    }
  }
  console.log(total);
}
<label>Please enter your flight Number:</label><br>
<input type="text" id="flightNumber" name="flightnumber" value="" /> <br />
<label>Please enter Miles Flown:</label><br>
<input type="text" id="milesFlown" name="milesflown" value="" />
<br>
<input type="button" id="display" name="display" value="Submit Flight Information" />
<br>
<div id="classMessages"></div>
<input type="button" id="status" name="status" value="Get Class Status" onclick=getClassStatus(); />
<table id="flightTable">
  <tr>
    <th>Flight Number</th>
    <th>Miles Flown</th>
  </tr>
</table>
</div>

最新更新