使用javascript添加相乘的数字

时间:2017-06-05 06:43:49

标签: javascript jquery html



function multiplyBy()
{
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
       num3= document.getElementById("result").value = num1 * num2;
       document.getElementById("total").value = +num3 ;
}

1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" value="" onchange="multiplyBy()" /><br>
<p>The Result is : <br>
<input type="text" name="result" id = "result" value=""/>
</p>
<p>Total :<br>
<input type="text" name="total" id="total" value=""/>
</p>
&#13;
&#13;
&#13;

&#13;
&#13;
function multiplyBy()
{
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
       num3= document.getElementById("result").value = num1 * num2;
}
&#13;
1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" value="" 
onchange="multiplyBy()" /><br>
<p>The Result is : <br>
<input type="text" name="result" id = "result" value=""/>
</p>
&#13;
&#13;
&#13;

我在这里乘以两个数字。如何在另一个文本框中添加这些相乘的数字。假设2&amp; 5乘以4&amp; 6乘以如何添加这些数字。

4 个答案:

答案 0 :(得分:0)

如果要计算文本框输入的更改,则需要添加事件处理程序,如下所示。

&#13;
&#13;
function multiplyBy()
{
     var result = document.getElementById("result");
      var  num1 = document.getElementById("firstNumber").value;
       var num2 = document.getElementById("secondNumber").value;
     
     var num3= parseFloat(num1) * parseFloat(num2);// to convert entered values to float and || 0 to  use 0 if no value is there
       
       if(!isNaN(num3)){
          document.getElementById("resultPrev").value =   document.getElementById("result").value;
           document.getElementById("resultCurrent").value =   num3;
             result.value =  parseFloat(result.value || 0) + num3;
       }
        
       
}


document.getElementById("firstNumber").addEventListener("change",multiplyBy);


document.getElementById("secondNumber").addEventListener("change",multiplyBy);
&#13;
1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<p>The Previous Result is : <br>
<input type="text" name="resultPrev" id = "resultPrev" value="0"/>
</p>
<p>The Current Result is : <br>
<input type="text" name="resultCurrent" id = "resultCurrent" value=""/>
</p>
<p>The Final Result is : <br>
<input type="text" name="result" id = "result" value=""/>
</p>
&#13;
&#13;
&#13;

考虑处理非数字值。

答案 1 :(得分:0)

试试这个。您需要使用parseFloat解析输入值。这会将字符串转换为数字。将现有结果值添加到新计算值中。 |0用于输入0的错误值

已更新

  1. 添加了prev,present和total结果框
  2. 使用新结果附加每个附加输入。

    &#13;
    &#13;
    var last=0;
    function multiplyBy() {
      var num1 = document.getElementById("firstNumber");
      var num2 = document.getElementById("secondNumber");
      var prev = document.getElementById("prev");
      var present = document.getElementById("present");
      var total = document.getElementById("result");
      prev.value=last;
      present.value=(parseFloat(num1.value) * parseFloat(num2.value))
      last = last+(parseFloat(num1.value) * parseFloat(num2.value))
      total.value=last;
      num1.value = ""; 
      num2.value = "";
    
      }
    &#13;
    1st Number : <input type="text" id="firstNumber" value="" /><br> 2nd Number: <input type="text" id="secondNumber" value="" onchange="multiplyBy()" /><br>
    <p >
    Prev result:<br>
    <input type="text" name="result"  id="prev" ><br>
    Present:<br>
    <input type="text" name="result"  id="present" ><br>
    The total Result is : <br>
    <input type="text" name="result"  id="result"><br>
    </p>
    &#13;
    &#13;
    &#13;

答案 2 :(得分:0)

我已将multiplyBy()添加到两个输入字段,并将类型更改为数字以限制用户仅输入数字。

请查看下面的代码段。

&#13;
&#13;
document.getElementById("firstNumber").addEventListener("change",multiplyBy);
document.getElementById("secondNumber").addEventListener("change",multiplyBy);

function multiplyBy()
    {
    actualResult = document.getElementById("actualResult").value ? document.getElementById("actualResult").value : 0; // Return zero if there is no value in actualResult input field
            num1 = document.getElementById("firstNumber").value;
            num2 = document.getElementById("secondNumber").value;       
           num3= document.getElementById("result").value = num1 * num2;
 document.getElementById("actualResult").value = Number(actualResult) + num3;  
    }
&#13;
 1st Number : <input type="number" id="firstNumber" value=""/><br>
    2nd Number: <input type="number" id="secondNumber" value=""/><br>
    <p>The Current Result is : <br>
    <input type="number" name="result" id = "result" value=""/>
 </p>
   <p>Previous Result + Current Result : <br> 
    <input type="number" name="actualResult" id = "actualResult" value=""/>
    </p>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您只需要获取文本框的值并使用结果

添加它
function multiplyBy()
{       var sum = 0;
        var num=0; 
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
        num =parseInt(num1 * num2);
        num3=parseInt(document.getElementById("result").value);
        document.getElementById("result").value=(num + num3);

}
</script>
1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" value="" 
onchange="multiplyBy()" /><br>
<p>The Result is : <br>
<input type="text" name="result" id = "result" value="0"/>
</p>