刚开始学编程,下面是我编写代码遇到的问题。
<button type="button" onclick="operation()">计算</button>
这段代码运行后的点击按钮无法执行。
<body>
<script type="text/javascript">
//输入数字1选择运算符输入数字2点击计算得出正确的结果并填充到结果文本框中
function operation()
{
var n1 = document.getElementById('num1');
var n2 = document.getElementById('num2');
var n3 = document.getElementById('num3');
if(sym.value == '+'){
n3.value = parseInt(n1) + parseInt(n2);
} else if(sym.value == '-') {
n3.value = parseInt(n1) - parseInt(n2);
} else if(sym.value == '*') {
n3.value = parseInt(n1) * parseInt(n2);
} else if(sym.value == '/'&&num_2!=0) {
n3.value = parseInt(n1) / parseInt(n2);
} else if(sym.value == '%'&&num_2!=0) {
n3.value = parseInt(n1) % parseInt(n2);
}
}
</script>
数字1:<input type="text" id="num1" />
<select id="sym">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
数字2:<input type="text" id="num2" /> =结果:
<input type="text" id="num3 " />
<button type="button" onclick="operation()">计算</button>
</body>
麻烦帮忙看下哪里出了问题呢。