我是Java Script的新學習者。我的數學等式有問題。它顯示錯誤的答案。 請幫我找到這個錯誤。java腳本顯示錯誤答案的數學方程
<html>
<head>
\t <title>calculation of (p+r)*(n+r) </title>
<script>
function calculate()
{
var p = document.getElementById("p").value;
var n = document.getElementById("n").value;
var r = document.getElementById("r").value;
\t \t \t
var calculationa= p+r;
\t \t \t
\t \t \t var calculationb= n+r;
\t \t \t
\t \t \t
\t \t \t var cal_final=Number(calculationa)*Number(calculationb);
\t \t \t
\t \t \t document.getElementById("result").innerHTML=cal_final;
}
</script>
</head>
<body>
<h1>calculation of (p+r)*(n+r)</h1>
p: <input id="p"><br/>
n: <input id="n"><br/>
r: <input id="r"><br/>
<button onclick="calculate()">calculate</button>
\t <p id="result"></p>
</body>
</html>
這是我要解決的方程。 數學顯示這個結果是不正確的。 http://i.stack.imgur.com/Ir9hs.png
我該如何解決這個問題?
提示:所有的'.value's是字符串,即使他們看起來像數字。 – georg
在'p'之前的'+'會將它轉換爲'Number''var calculationa = + p + r; var calculationb = + n + r;' - 更新這兩行! –
[加法不能在JavaScript中工作]可能的重複(http://stackoverflow.com/questions/8377410/addition-is-not-working-in-javascript) –