[英]Uncaught ReferenceError: function is not defined at HTMLInputElement.onclick
I have a problem with my code, It's a currency calculator and u can store your results, and it's work fine but I also want to make it able to delete result, function removeRow should do that but when I'm trying to use I have a console error like: 我的代码有问题,这是一个货币计算器,您可以存储您的结果,它工作正常,但我也想使其能够删除结果,函数removeRow应该可以,但是当我尝试使用时控制台错误,例如:
Uncaught ReferenceError: removeRow is not defined
at HTMLInputElement.onclick (index.html:1)
I've tried everything I found and nothing helps 我已经尝试了所有发现的内容,但无济于事
Here's my code: 这是我的代码:
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);
}
});
and HTML: 和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 € = <input type="text" value="4.14" id="euroValue">PLN</p>
<input type="text" placeholder="Nazwa operacji" id="operationName">
<input type="text" placeholder="ilość €" id="operationValue">
<input type="submit" placeholder="Oblicz" id="operationConfirm">
<table id="results">
<thead>
<tr>
<th>Nazwa operacji</th>
<th>Kwota €</th>
<th>Kwota PLN</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
</table>
<script type="text/javascript" src="main.js"></script>
</div>
</body>
</html>
And here's codepen: 这是codepen:
https://codepen.io/jkarkosza/pen/qpoBEv?editors=1010 https://codepen.io/jkarkosza/pen/qpoBEv?editors=1010
Just move the function removeRow(btn)
-Definition out of the EventListener-Definition. 只需将function removeRow(btn)
-Definition从EventListener-Definition中移出即可。 When you create a function inside that scope it will not be visible outside that function. 当您在该范围内创建函数时,在该函数外部将不可见。
document.addEventListener("DOMContentLoaded", function(event) {
[...]
});
function removeRow(btn) {
const thisTable = document.getElementById('tableBody');
thisTable.deleteRow(btn.parentNode.rowIndex-1);
}
Just declare the removeRow
method outside the addEventLister
. 只需在addEventLister
之外声明removeRow
方法。 It works. 有用。
On clicking the button, it looks for the function in the global scope. 单击按钮后,它将在全局范围内查找功能。 So you need to declare globally. 因此,您需要全局声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.