gpt4 book ai didi

javascript - 如何检索表单值,对其执行操作并显示在

标签中?

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:41 24 4
gpt4 key购买 nike

我想从 form-control type = "number"中获取一个数字,比较它,然后如果符合条件,则对其执行乘法和除法。之后我想将结果显示为 <p> 的内容标签。如何实现?

到目前为止,这是我尝试过的:

let amount = document.getElementById(".amount");
let marginAmount = document.getElementById(".marginAmount")
let submit = document.getElementById(".submit");
submit.addEventListener('click', calcAmount);

const calcLevel1 = amount * 7 / 100;

function calcAmount() {
let userAmount = Number(amount.value);
let level1 = 351;
if (userAmount <= level1) {
marginAmount.textContent = calcLevel1.value;
}
}
<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>

最佳答案

这里是修正的代码,有。哪些要删除我还添加了 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>

关于javascript - 如何检索表单值,对其执行操作并显示在 <p> 标签中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55758874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com