为了账号安全,请及时绑定邮箱和手机立即绑定

如何检索表单值,对该表单值执行操作并在<p>标记中显示?

如何检索表单值,对该表单值执行操作并在<p>标记中显示?

芜湖不芜 2021-04-26 22:17:14
我想从表单控件类型=“数字”中获取一个数字,将其进行比较,然后如果条件确定,则对其进行乘法和除法。之后,我想将结果显示为<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>
查看完整描述

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>


查看完整回答
反对 回复 2021-05-06
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

三件事:

  • .在ID选择器中没有这些选择器(它们应该存在,#但只能在一种querySelector方法中使用)。

  • 您正在calcLevel1直接计算,这意味着0 * 7 / 1000

  • 你需要计算calcLevel1amount.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>


查看完整回答
反对 回复 2021-05-06
  • 2 回答
  • 0 关注
  • 176 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信