需要在JAVA脚本中打印结果

时间:2017-10-25 15:39:08

标签: javascript html user-interface

我是Javascript的新手,我需要打印一个乘法函数的结果。请你帮我做。 请找到附件图片

以下是代码:



function multiply() {
  var a = document.getelemtById("weight").value;
  var b = document.getelemtById("cost").value;
  var myResult = document.getElementById('result');
  var myResult = parseint(a) * parseint(b);
}

<span>
  <center>
  <h3> Calculation of Shipping charge </h3>
  <label for = "weight"> Total Prodcut Weight </label>
  <input type = "text" name = "Total Prodcut Weight" id = "weight" > <br>

  <br>

  <label for = "cost" > Shipping cost(per kg) < /label>
  <input type = "text" name = "Shipping cost(per kg)" id = "cost" >
  <br>
  <br>
  <button type = "button" onclick = multiply() > compute </button>

<br>
<div id="result"></div>
&#13;
&#13;
&#13;

enter image description here

2 个答案:

答案 0 :(得分:0)

根据评论建议,将getelemtById更改为getElementById并将parseint更改为parseInt。我还更改了.innerHTML #result以显示结果。

&#13;
&#13;
<script type="text/javascript">
  function multiply() {
    var a = document.getElementById("weight").value;
    var b = document.getElementById("cost").value;
    var myResult = document.getElementById('result');
    myResult.innerHTML = parseInt(a) * parseInt(b); // removed var
  }
</script>
<span>
<center>
<h3>Calculation of Shipping charge</h3>
<label for="weight">Total Prodcut Weight</label> <!-- Prodcut should be Product-->
   <input type="text"  name="Total Prodcut Weight" id="weight"><br>

   <br>

       <label for="cost">Shipping cost(per kg)</label>
<input type="text"  name="Shipping cost(per kg)" id="cost">  
<br>
<br>
 <button type="button" onclick=multiply()>compute</button>
</center>
                </span>
<br>
<div id="result">
  <center>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我已编辑您的代码尝试

&#13;
&#13;
function multiply() {
  var a = document.getElementById("weight").value;
  var b = document.getElementById("cost").value;
  //var myResult = document.getElementById('result');
  var myResult = parseInt(a) * parseInt(b);
  document.getElementById('result').innerHTML=myResult
  console.log(myResult)
}
&#13;
<h3>Calculation of Shipping charge</h3>
<label for="weight">Total Prodcut Weight</label>
<input type="text" name="Total Prodcut Weight" id="weight"><br>

<br>

<label for="cost">Shipping cost(per kg)</label>
<input type="text" name="Shipping cost(per kg)" id="cost">
<button type="button" onclick=multiply()>compute</button>
<div id="result"></div>
&#13;
&#13;
&#13;