第三盒锁定值JS

时间:2018-11-08 08:32:26

标签: javascript html

我正在使用html中的javascript函数脚本来将两个输入框的乘积转换为第三个。 HTML:-

<input type="text" class="form-control" id="inputBuyPrice" placeholder="0.0" name="price" oninput="calculate()">
<input type="text" class="form-control" id="inputBuyAmount" placeholder="0.0" name="amount" oninput="calculate()">
<input type="text" class="form-control" id="inputBuyTotal" placeholder="0.0" name="total" oninput="calculate()">

和javascript:-

  <script>
  function calculate() {
var myBox1 = document.getElementById('inputBuyAmount').value; 
var myBox2 = document.getElementById('inputBuyPrice').value;
var result = document.getElementById('inputBuyTotal'); 
var numb = myBox1 * myBox2;
numb = numb.toFixed(8);
result.value = numb;
}
 </script>

现在一切正常。但是总价值受数量和价格乘积的锁定。我不希望它是只读的,但是输入的值可以由用户更改。

2 个答案:

答案 0 :(得分:0)

在评论中您说过:

  

简单的事物数量*价格要总计显示,但是总计可以手动更改,以防万一价格或数量再次发生变化,产品应该总计显示

如果这是您想要的,只需删除oninput="calculate()"上的inputBuyTotal。这将允许用户根据需要进行更改,但是更改inputBuyPriceinputBuyAmount会将其更改为这些字段的乘积。


(我希望从我购买的商店让我手动更改要支付的总额...)

答案 1 :(得分:0)

更改

oninput="calculate()" 

oninput="calculateAmount()" 

并添加函数calculateAmount()