[英]HTML and Javascript making a simple addition calculator
I'm trying to make a simple addition calculator.我正在尝试制作一个简单的加法计算器。 You put two values into the boxes and click the "+" sign and the third box has the sum.您将两个值放入框中,然后单击“+”号,第三个框有总和。 However, it looks like everything is right to me but when I click the + sign nothing happens但是,看起来一切对我来说都是正确的,但是当我单击 + 号时没有任何反应
First you should change getElementsById()
to getElementById()
because there is only one id per element.首先,您应该将getElementsById()
更改为getElementById()
,因为每个元素只有一个 id。
<input type="text" id="box1"><br/> <input type="text" id="box2"><br/> <input type="text" id="+"><br/> <input type="button" value="+" onclick="calcSum()"><br/> <script> function calcSum(){ let box1 = document.getElementById("box1").value; let box2 = document.getElementById("box2").value; let sum = Number(box1) + Number(box2); document.getElementById("+").value = sum; } </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.