<!--不使用parseInt()函数-->
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>switch</title>
<script type="text/JavaScript">
function f(){
var myweek=document.getElementById("a").value; //myweek表示星期几变量
document.write("结果:"+myweek);
document.write("类型:"+typeof(myweek));
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:
alert("输入错误,不在1~7之间");
}
}
</script>
</head>
<body>
<h3>请输入工作日</h3>
<input type="text" id="a">
<input type="button" value="提交日期" onclick=f()>
</body>
</html>
<!--使用parseInt()函数-->
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>switch</title>
<script type="text/JavaScript">
function f(){
var myweek=parseInt(document.getElementById("a").value); //myweek表示星期几变量
document.write("结果:"+myweek);
document.write("类型:"+typeof(myweek));
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:
alert("输入错误,不在1~7之间");
}
}
</script>
</head>
<body>
<h3>请输入工作日</h3>
<input type="text" id="a">
<input type="button" value="提交日期" onclick=f()>
</body>
</html>
var myweek=document.getElementById("a").value 此时得到的id为a的text框的值是一个string变量,那么case的判断条件也该是字符串变量需要加“”!因为应该是text框的输入之后就是以字符串变量存储的.
2017/10/21 发现一个可以将字符串转化成整数的函数parseInt()