<!DOCTYPE html> <html> <head> <title> 事件</title> </head> <body> <input type='text' id='txt1' /> <select id='select'> <option value='+'>+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type='text' id='txt2' /> <input type='button' value=' = ' onclick="count()" /> <!--通过 = 按钮来调用创建的函数,得到结果--> <input type='text' id='fruit' /> <script type="text/javascript"> var a = parseInt(document.getElementById('txt1')).value; var b = parseInt(document.getElementById('txt2')).value; var c = document.getElementById('select').value; var d; function count() { if (c == '+') { d = a + b; document.getElementById('fruit').value = d; } else if (c == '-') { d = a - b; document.getElementById('fruit').value = d; } else if (c == '*') { d = a * b; document.getElementById('fruit').value = d; } else if (c == '/') { d = a / b; document.getElementById('fruit').value = d; } else if (isNaN(a) || isNaN(b)) { alert('请输入数字!'); } else if (a.length == 0) { alert('请输入数字!'); } else if (b.length == 0) { alert('请输入数字!'); } else if (b == 0) { alert('除数不能为0!'); } </script> </body> </html>
为什么执行一直有问题,输出一直是NaN?
玩意11
相关分类