<!DOCTYPE html> <html> <head> <title> 事件</title> <script type="text/javascript"> function count(){ var n1 = parseInt( document.getElementById("text1").value); var n2 = parseInt( document.getElementById("text2").value); var ff = document.getElementById("select").value; var rs = ""; switch (ff) { case '+': rs = n1 + n2; break; case '-': rs = n1 - n2; break; case '*': rs = n1 * n2; break; case '/': rs = n1 / n2; break; } //设置结果输入框的值 document.getElementById('fruit').value = rs; } </script> </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' /> </body> </html>
id为txt1 txt2你写错了,建议以后复制
定义变量rs有问题。
var rs;
就够了