如何在按键上将两个输入相乘并加第三个输入

时间:2019-09-13 11:56:32

标签: javascript

如何在按键上将两个输入相乘并将第三个输入相加 我有一个三输入字段,需要多个前两个输入和第三个输入的加法,然后显示结果字段 HTML代码:

<input type="text" tabindex="3" class="form-control" name="making_charge" oninput="calculate()" placeholder="Making Charge" id="box3" required />

 <input type="text" tabindex="3" class="form-control" name="total_price" placeholder="Total Price" id="result" readonly  />

function calculate() {
        var myBox1 = document.getElementById('box1').value; 
        var myBox2 = document.getElementById('box2').value;
        var myBox3 = document.getElementById('box3').value;
        var result = document.getElementById('result'); 
        var myResult = myBox1 * myBox2 + myBox3;
        result.value = myResult; 
    }

1 个答案:

答案 0 :(得分:0)

function UpperWithCycle(str) { wordArray = str.split(" "); for (let i = 0; i < wordArray.length; i++) { wordArray[i] = wordArray[i].substring(0,1).toUpperCase() + wordArray[i].substring(1).toLowerCase(); } return wordArray.join(" "); } console.log(UpperWithCycle('hello everyone my name is Andrew, how are you doing?'));是一个字符串,因此您需要将其隐藏为数字以对其进行数学运算。

您可以使用value+Number()parseInt()

parseFloat()
function calculate() {
  var myBox1 = +document.getElementById('box1').value;
  var myBox2 = +document.getElementById('box2').value;
  var myBox3 = +document.getElementById('box3').value;
  var result = document.getElementById('result');
  var myResult = myBox1 * myBox2 + myBox3;
  result.value = myResult;
}