javascript - 未捕获的 ReferenceError : function is not defined at HTMLInputElement. onclick

标签 javascript html function

我的代码有问题,它是一个货币计算器,你可以存储你的结果,它工作正常但我也想让它能够删除结果,函数 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>

这是代码笔:

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

最佳答案

只需将 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);
}

关于javascript - 未捕获的 ReferenceError : function is not defined at HTMLInputElement. onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182282/

相关文章:

html - 当我将 li 列表向右浮动时,它是居中对齐的

javascript - 使用上一个/下一个按钮导航 div

c - a[i] 真的和 C 中的 *(a + i) 一样吗?

javascript - 多个条目后字符串替换然后拆分

javascript - 错误: Invariant Violation: addComponentAsRefTo when using react-router

javascript - 是否可以模拟 iframe?

javascript - PHP 包含在 DIV 样式背景中

java - 使用动态字符串工作函数?

javascript - 访问渲染中从 react 函数返回的变量(在循环中)

javascript - 使用 ajax 分页加载页面后重新初始化其他 javascript 函数