為什么輸入后提示都的是default的值?。?/h1>
<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
<title>switch</title>
<script?type="text/JavaScript">
function?day(){
var?myweek?=prompt("what?day?is?it?today?","1");
switch(myweek)
{
?case?1:
?case?2:
?document.write("學(xué)習(xí)理念知識(shí)");
?break;
?case?3:
?case?4:
?document.write("到企業(yè)實(shí)踐");
?break;
?case?5:
?document.write("總結(jié)經(jīng)驗(yàn)");
?break;
?case?6:
?case?7:
?document.write("周六、日休息和娛樂(lè)");
?break;
default:alert("input?error!");}
}
</script>
</head>
<body>
<input?type="button"?value="click?here!"?onClick="day()">
</body>
</html>
<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
<title>switch</title>
<script?type="text/JavaScript">
function?day(){
var?myweek?=prompt("what?day?is?it?today?","1");
switch(myweek)
{
?case?1:
?case?2:
?document.write("學(xué)習(xí)理念知識(shí)");
?break;
?case?3:
?case?4:
?document.write("到企業(yè)實(shí)踐");
?break;
?case?5:
?document.write("總結(jié)經(jīng)驗(yàn)");
?break;
?case?6:
?case?7:
?document.write("周六、日休息和娛樂(lè)");
?break;
default:alert("input?error!");}
}
</script>
</head>
<body>
<input?type="button"?value="click?here!"?onClick="day()">
</body>
</html>
2015-08-14
這里接收到的值是字符串,下面匹配的卻是整型,2種解決方法。
一,直接把接收到的值強(qiáng)制轉(zhuǎn)換為數(shù)字類(lèi)型,如:
var?myweek?=parseInt(prompt("what?day?is?it?today?","1"));二,把下面所有case語(yǔ)句后面的值改為字符串,即加引號(hào),如:
switch(myweek) ????{ ????????case?"1": ????????case?"2": ????????????document.write("學(xué)習(xí)理念知識(shí)"); ????????????break; ????????case?"3": ????????case?"4": ????????????document.write("到企業(yè)實(shí)踐"); ????????????break; ????????case?"5": ????????????document.write("總結(jié)經(jīng)驗(yàn)"); ????????????break; ????????case?"6": ????????case?"7": ????????????document.write("周六、日休息和娛樂(lè)"); ????????????break; ????????default:alert("input?error!");} }