2 回答
TA贡献1921条经验 获得超9个赞
这是已更正的代码,有。我还添加math.round了哪些要删除的内容以获得四舍五入的结果:
<html>
<head>
<script>
function calcAmount() {
let userAmount = document.getElementById("amount").value;
let level1 = 351;
if (userAmount <= level1) {
document.getElementById("marginAmount").textContent = Math.round(userAmount * 7) / 100;
} else {
document.getElementById("marginAmount").textContent = "Amount is > 351";
}
}
</script>
</head>
<body>
<div class="form">
<label for="points amount">Entrez un montant en points</label>
<input type="number" class="amount-enter" id="amount">
<input type="submit" value="Montant marge additionnelle" id="submit" class="submit-enter" onclick="calcAmount()">
</div>
<p id="marginAmount" class="enter-margin"></p>
</body>
TA贡献1871条经验 获得超13个赞
三件事:
您
.
在ID选择器中没有这些选择器(它们应该存在,#
但只能在一种querySelector
方法中使用)。您正在
calcLevel1
直接计算,这意味着0 * 7 / 100
是0
。你需要计算
calcLevel1
用amount.value
,这意味着你可以删除calcLevel1.value
,当你设置textContent
的段落。
我还添加了一条else
语句,以便textContent
在该语句为false时将其清空。
let amount = document.getElementById("amount");
let marginAmount = document.getElementById("marginAmount")
let submit = document.getElementById("submit");
submit.addEventListener('click', calcAmount);
function calcAmount() {
const calcLevel1 = amount.value * 7 / 100;
let userAmount = Number(amount.value);
let level1 = 351;
if (userAmount <= level1) {
marginAmount.textContent = calcLevel1;
} else {
marginAmount.textConten = "";
}
}
<div class="form">
<label for="points amount">Entrez un montant en points</label>
<input type="number" class="amount-enter" id="amount">
<input type="submit" value="Montant marge additionnelle" id="submit" class="submit-enter">
</div>
<p id="marginAmount" class="enter-margin"></p>
添加回答
举报