我是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;
答案 0 :(得分:0)
根据评论建议,将getelemtById
更改为getElementById
并将parseint
更改为parseInt
。我还更改了.innerHTML
#result
以显示结果。
<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;
答案 1 :(得分:0)
我已编辑您的代码尝试
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;