[英]JavaScript problems with calculator
I have a little problem... if I run my script in html everything works fine but if I add numbers on the bars that I made and press the equal button nothing happens. 我有一个小问题...如果我在html中运行脚本,一切正常,但是如果我在自己制作的条形上添加数字并按下等号按钮,则不会发生任何事情。 I don't know what's the problem
我不知道怎么了
here's my code 这是我的代码
<!DOCTYPE html>
<html>
<head>
<title>Kalkulaator</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale1.0">
<script>
function calc()
{
var number1 = parseInt(document.getElementById('number1').value);
var number2 = parseInt(document.getElementById('number2').value);
var oper = document.getElementById('operaatorid').value;
if(oper === '+')
{
document.getElementById('tulemus').value = number1+number2;
}
</script>
</head>
<body>
<input type="text" id="number1"/><br/><br/>
<input type="text" id="number2"/><br/><br/>
<select id="operaatorid">
<option value=="+">+</option>
</select>
<button onclick="calc();">=</button>
<input type="text" id="tulemus"/>
</body
</html>
You have a few problems with your code, as said by Alex K. in his comment . 正如Alex K.在他的评论中所说,您的代码有一些问题。
You have two equal symbols ( =
) on your <option>
and your Javascript function is missing a closing bracket ( }
). <option>
上有两个相等的符号( =
),并且Javascript函数缺少右括号( }
)。
This is the corrected snippet: 这是更正后的代码段:
function calc() { var number1 = parseInt(document.getElementById('number1').value); var number2 = parseInt(document.getElementById('number2').value); var oper = document.getElementById('operaatorid').value; if (oper === '+') { document.getElementById('tulemus').value = number1 + number2; } }
<input type="text" id="number1" /><br/><br/> <input type="text" id="number2" /><br/><br/> <select id="operaatorid"> <option value="+">+</option> </select> <button onclick="calc();">=</button> <input type="text" id="tulemus" />
Also, in HTML5, you don't need to close input tags, so you can simply have <input type="text">
. 另外,在HTML5中,您无需关闭输入标签,因此只需具有
<input type="text">
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.