<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>switch</title><script type="text/JavaScript">function liang(){var myweek =document.getElementById("bian").value;switch(myweek){ case 1: case 2: document.write("学习理念知识"); break; case 3: case 4: document.write("到企业实践"); break; case 5: document.write("总结经验"); break; case 6: case 7: document.write("周六、日休息和娱乐"); break; default: document.write("输入出错");}}</script></head><body><input id="bian" type="text" name="grade" ><br><input type="button" value="点吧" onclick="liang()"></body></html>为什么框里写什么,都是显示错误。
2 回答
已采纳
Perona
TA贡献355条经验 获得超403个赞
从页面获取的都是字符串,2种解决方法:
把case后的值加上引号,变成字符串型,如:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>switch</title> <script type="text/JavaScript"> function liang() { var myweek =document.getElementById("bian").value; switch(myweek) { case '1': case '2': document.write("学习理念知识"); break; case '3': case '4': document.write("到企业实践"); break; case '5': document.write("总结经验"); break; case '6': case '7': document.write("周六、日休息和娱乐"); break; default: document.write("输入出错"); } } </script> </head> <body> <input id="bian" type="text" name="grade" ><br> <input type="button" value="点吧" onclick="liang()"> </body> </html>
2.把获取的值转换成整型,如:
var myweek =parseInt(document.getElementById("bian").value);
添加回答
举报
0/150
提交
取消