【问题标题】:Addition, Subtraction and Division simultaneously on JavaScript在 JavaScript 上同时进行加法、减法和除法
【发布时间】:2019-10-29 21:20:52
【问题描述】:

我有这个文本框:

<div class="form-group">
  <label>Enter Sum of Cleared</label>
  <input type="text" name="sum_cleared" id="sum_cleared" class="form-control" />
</div>

我想将 sum_cleared 除以 0.85,然后将结果乘以 0.94,然后从 sum_cleared 中输入的原始值中减去它,并显示最终结果:

<div class="form-group">
  <label>Sum of Total</label>
  <input type="text" name="sum_total" id="sum_total" class="form-control total" readonly/>
</div>

我想使用 onchange 和 oninput 事件动态地执行此操作,以便在用户在 sum_cleared 上键入值时更新 sum_total 的值。

最简单的方法是什么?

非常感谢

【问题讨论】:

  • 你试过什么代码?您已经说过要在 onchange 或 oninput 事件中执行此操作(提示:onchange 将等到该框在触发之前未聚焦; oninput 将在每次输入更改时立即触发)。除法使用运算符/,乘法使用运算符*,减法使用运算符-,所有数学都可以用括号(圆括号,就像它所在的位置)分组,以指定首先计算的内容。
  • sum_cleared - (sum_cleared / 0.85) * 0.94?
  • 致投反对票的人:不要立即投反对票,请注意这是一个全新的 Stack Overflow 用户,也许只是对您的问题发表评论。
  • @IceMetalPunk 我能够按照您提到的运营商做到这一点,请看我的回答!非常感谢
  • @ScottMarcus 感谢您的支持,我希望在没有任何意见的情况下我不会这么快就被否决:(我能够找出答案,我刚刚发布了它

标签: javascript html division addition


【解决方案1】:

我能够做到以下几点:

<div class="form-group">
    <label>Enter Sum of Cleared</label>
    <input type="text" name="sum_cleared" id="sum_cleared"
           class="form-control"
           oninput="GetTotal(this.value)" onchange="GetTotal(this.value)"/>
</div>
<div class="form-group">
    <label>Total Commission</label>
    <input type="text" name="sum_total" id="sum_total"
           class="form-control total" readonly/>
</div>

然后添加一个小脚本:

<script type="text/javascript">
function GetTotal(valNum) {
    document.getElementById("sum_total").value=valNum/0.85*0.94-valNum
}
</script>

【讨论】:

    【解决方案2】:

    你可以这样做:

    const input = document.querySelector('#sum_cleared');
    const output = document.querySelector('#sum_total');
    
    
    input.addEventListener('input', update);
    input.addEventListener('change', update);
    
    function update() {
       const updated = input.value - ((input.value / 0.85) * 0.94);
       output.value = updated;
    }
     <div class="form-group">
                        <label>Enter Sum of Cleared</label>
                        <input type="text" name="sum_cleared" id="sum_cleared" class="form-control" />
    </div>
     <div class="form-group">
                        <label>Sum of Total</label>
                        <input type="text" name="sum_total" id="sum_total" class="form-control total" readonly/>
                    </div>

    【讨论】:

      猜你喜欢
      • 2013-07-30
      • 2022-08-11
      • 1970-01-01
      • 2014-10-22
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多