开发者问题收集

未捕获的 ReferenceError:函数未在 HTMLInputElement.onclick 中定义

2018-01-10
16066

我的代码有问题,它是一个货币计算器,你可以存储你的结果,它工作正常,但我还想让它能够删除结果,函数 removeRow 应该可以做到这一点,但是当我尝试使用时,我有一个控制台错误,如:

Uncaught ReferenceError: removeRow is not defined
at HTMLInputElement.onclick (index.html:1)

我已经尝试了所有我发现的方法,但都无济于事

这是我的代码:

document.addEventListener("DOMContentLoaded", function(event) {
console.log("DOM is ready");

const operationConfirm = document.getElementById('operationConfirm');
const resultList = document.getElementById('tableBody');

const operation = {
  euroValue: document.getElementById('euroValue'),
  operationName: document.getElementById('operationName'),
  operationValue: document.getElementById('operationValue')
}

operation.euroValue.onchange = updateValues;

operationConfirm.addEventListener("click", function() {
  let thisOperationValue = operation.operationValue.value;
  let thisOperationName = operation.operationName.value;
  let thisEuroValue = parseFloat(operation.euroValue.value);

  let calcResoult = calcCurrency(thisEuroValue, thisOperationValue);
  let newOperation = document.createElement('tr');

  newOperation.innerHTML = `
  <td>${thisOperationName}</td>
  <td class="amountToChange">${thisOperationValue}</td>
  <td class="resultValue">${calcResoult.toFixed(2)} PLN</td>
  <input type="button" value="X" onclick="removeRow(this)" id="deleteBtn"/>`;
  const btns = resultList.getElementsByTagName('input');
  const delBtns = Array.from(btns);
  for( let i = 0; i < delBtns.length; i++) {
    delBtns[i].setAttribute('click', 'removeRow(this)');
  }

  resultList.appendChild(newOperation);

});

function calcCurrency(eValue, quantity) {
  return quantity * eValue;
}

function updateValues() {
  const outComePLN = Array.from( document.getElementsByClassName('resultValue'));
  const outComeEur = Array.from(document.getElementsByClassName('amountToChange'));
  for(let i = 0; i < outComePLN.length; i++) {
    outComePLN[i].innerHTML = (outComeEur[i].innerHTML * operation.euroValue.value).toFixed(2);
  }
};

function removeRow(btn) {
  const thisTable = document.getElementById('tableBody');
  thisTable.deleteRow(btn.parentNode.parentNode.rowIndex);
}
});

和 HTML:

 <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Klkulator walut</title>
    </head>
    <body>
  <style>
    #cantor{
      margin: 10vh auto 0 auto;
      border: 1px solid black;
      width: 50%;
      min-height: 300px;
    }
  </style>
  <div id="cantor">
    <p>1 &euro; = <input type="text" value="4.14" id="euroValue">PLN</p>
    <input type="text" placeholder="Nazwa operacji" id="operationName">
    <input type="text" placeholder="ilość &euro;" id="operationValue">
    <input type="submit" placeholder="Oblicz" id="operationConfirm">
    <table id="results">
      <thead>
        <tr>
          <th>Nazwa operacji</th>
          <th>Kwota &euro;</th>
          <th>Kwota PLN</th>
        </tr>
      </thead>
      <tbody id="tableBody">
      </tbody>
    </table>
    <script type="text/javascript" src="main.js"></script>
  </div>
</body>
</html>

这是 codepen:

https://codepen.io/jkarkosza/pen/qpoBEv?editors=1010

2个回答

只需将 function removeRow(btn) -Definition 移出 EventListener-Definition 即可。当您在该范围内创建一个函数时,该函数在该函数之外将不可见。

document.addEventListener("DOMContentLoaded", function(event) {
   [...]
});

function removeRow(btn) {
  const thisTable = document.getElementById('tableBody');
  thisTable.deleteRow(btn.parentNode.rowIndex-1);
}
Martin Schneider
2018-01-10

只需在 addEventLister 之外声明 removeRow 方法即可。它有效。

单击按钮时,它会在全局范围内查找该函数。因此您需要全局声明。

Vignesh Raja
2018-01-10