提问人:Umer Rashid 提问时间:11/17/2023 最后编辑:tacoshyUmer Rashid 更新时间:11/17/2023 访问量:37
我在 WordPress 自定义 HTML 上遇到 NaN 错误
I am facing he NaN error on WordPress Custom HTML
问:
当我在代码编辑器中执行代码时,我的代码有问题,它工作正常,但是当在 WordPress 中执行代码时,代码运行不佳,它给出了 NaN 错误。
代码在 W3School 代码编辑器上工作正常
当它在WordPress上运行代码时,它会给出以下错误:
function calculateBill() {
const units = parseFloat(document.getElementById('units').value);
const lessThan200 = document.getElementById('lessThan200').checked;
// Rates
const perUnit = 50;
const fcSurcharge = 0.43;
const electricityDuty = 0.20;
const tvFee = 35;
const gst = 2.40;
const njSurcharge = 0.1;
let totalBill = (perUnit + fcSurcharge + electricityDuty + gst + njSurcharge) * units + tvFee;
if (lessThan200 && units <= 200) {
totalBill *= 0.5; // Assuming 50% discount if less than 200 units for the last 6 months
}
const tableBody = document.getElementById('tableBody');
tableBody.innerHTML = `
<tr>
<td>Cost of electricity (${units} x ${perUnit})</td>
<td>${(units * perUnit).toFixed(2)}</td>
</tr>
<tr>
<td>F.C Surcharge</td>
<td>${(fcSurcharge * units).toFixed(2)}</td>
</tr>
<tr>
<td>Electricity Duty</td>
<td>${(electricityDuty * units).toFixed(2)}</td>
</tr>
<tr>
<td>TV Fee</td>
<td>${tvFee.toFixed(2)}</td>
</tr>
<tr>
<td>GST</td>
<td>${(gst * units).toFixed(2)}</td>
</tr>
<tr>
<td>N.J Surcharge</td>
<td>${(njSurcharge * units).toFixed(2)}</td>
</tr>
<tr>
<th>Total Estimated Bill</th>
<th>${totalBill.toFixed(2)}</th>
</tr>
`;
}
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f2f2f2;
}
.calculator {
background-color: #fff;
max-width: 100%;
width: 450px;
padding: 20px;
border-radius: 8px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
margin: 50px auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
button {
padding: 10px 15px;
background-color: #4CAF50;
color: #fff;
border: none;
cursor: pointer;
border-radius: 5px;
width: 100%;
}
<div>
<label for="units">Enter Units:</label>
<input type="number" id="units" placeholder="E.g. 200">
<br>
<label>
<input type="checkbox" id="lessThan200"> I am consuming 200 or less units for the last 6 months?
</label>
<br><br>
<button onclick="calculateBill()">Calculate</button>
<table>
<tbody id="tableBody">
<!-- Rows will be added here dynamically -->
</tbody>
</table>
</div>
答:
0赞
tacoshy
11/17/2023
#1
正如@CharlieSchliesser在评论中指出的那样,如果输入为空,则可能会发生错误。解决此问题的最简单方法是检查输入的数字实际上是数字还是 NaN。您可以使用该函数来执行此操作。然后,如果值是数字,则只需将逻辑反转为true:isNAN(units)
!isNaN()
function calculateBill() {
const units = parseFloat(document.getElementById('units').value);
if (!isNaN(units)) {
const lessThan200 = document.getElementById('lessThan200').checked;
// Rates
const perUnit = 50;
const fcSurcharge = 0.43;
const electricityDuty = 0.20;
const tvFee = 35;
const gst = 2.40;
const njSurcharge = 0.1;
let totalBill = (perUnit + fcSurcharge + electricityDuty + gst + njSurcharge) * units + tvFee;
if (lessThan200 && units <= 200) {
totalBill *= 0.5; // Assuming 50% discount if less than 200 units for the last 6 months
}
const tableBody = document.getElementById('tableBody');
tableBody.innerHTML = `
<tr>
<td>Cost of electricity (${units} x ${perUnit})</td>
<td>${(units * perUnit).toFixed(2)}</td>
</tr>
<tr>
<td>F.C Surcharge</td>
<td>${(fcSurcharge * units).toFixed(2)}</td>
</tr>
<tr>
<td>Electricity Duty</td>
<td>${(electricityDuty * units).toFixed(2)}</td>
</tr>
<tr>
<td>TV Fee</td>
<td>${tvFee.toFixed(2)}</td>
</tr>
<tr>
<td>GST</td>
<td>${(gst * units).toFixed(2)}</td>
</tr>
<tr>
<td>N.J Surcharge</td>
<td>${(njSurcharge * units).toFixed(2)}</td>
</tr>
<tr>
<th>Total Estimated Bill</th>
<th>${totalBill.toFixed(2)}</th>
</tr>
`;
} else {
// an error message logic should be included here
}
}
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f2f2f2;
}
.calculator {
background-color: #fff;
max-width: 100%;
width: 450px;
padding: 20px;
border-radius: 8px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
margin: 50px auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
button {
padding: 10px 15px;
background-color: #4CAF50;
color: #fff;
border: none;
cursor: pointer;
border-radius: 5px;
width: 100%;
}
<div>
<label for="units">Enter Units:</label>
<input type="number" id="units" placeholder="E.g. 200">
<br>
<label>
<input type="checkbox" id="lessThan200"> I am consuming 200 or less units for the last 6 months?
</label>
<br><br>
<button onclick="calculateBill()">Calculate</button>
<table>
<tbody id="tableBody">
<!-- Rows will be added here dynamically -->
</tbody>
</table>
</div>
评论
e
e
1e3
1,000
e
calculateBill()
e