求大神幫我看看代碼?為什么用switch不行
<!DOCTYPE?HTML>
<html>
????<head>
????????<meta?http-equiv="Content-Type"?content="text/html;?charset=gb2312">
????????<title>無標題文檔</title>
????</head>
????
????<body>
????????<form>
??????????請選擇你愛好:<br>
??????????<input?type="checkbox"?name="hobby"?id="hobby1">??音樂
??????????<input?type="checkbox"?name="hobby"?id="hobby2">??登山
??????????<input?type="checkbox"?name="hobby"?id="hobby3">??游泳
??????????<input?type="checkbox"?name="hobby"?id="hobby4">??閱讀
??????????<input?type="checkbox"?name="hobby"?id="hobby5">??打球
??????????<input?type="checkbox"?name="hobby"?id="hobby6">??跑步?<br>
??????????<input?type="button"?value?=?"全選"?onclick?=?"checkall();">
??????????<input?type="button"?value?=?"全不選"?onclick?=?"clearall();">
??????????<p>請輸入您要選擇愛好的序號,序號為1-6:</p>
??????????<input?id="wb"?name="wb"?type="text"?>
??????????<input?name="ok"?type="button"?value="確定"?onclick?=?"checkone();">
????????</form>
????????<script?type="text/javascript">
????????function?checkall(){
????????????var?hobby?=?document.getElementsByTagName("input");
???????????
??????????//?任務1
??????????for?(var?i=0;i<hobby.length-1;i++)
???????????hobby[i].checked=true;
????????}
????????function?clearall(){
????????????var?hobby?=?document.getElementsByName("hobby");
????????????
?????????//?任務2?
??????????for?(var?i=0;i<hobby.length-1;i++)
???????????hobby[i].checked=false;
????????????
????????}
??????
???????function?checkone(){
?????????j=0;
?????????var?j=parseInt(document.getElementById("wb").value);
???????
?????????//?任務3
?????????var?hobby?=?document.getElementsByName("hobby");
??????????
?????
????switch(j){
????case?1:?hobby[0].checked=true;
??????????????????????break;
????case?2:?hobby[1].checked=true;
?????????????????????break;
???case?3:?hobby[2].checked=true;
??????????????????????break;????????
????case?4:?hobby[3].checked=true;
????????????????????break;????????
????case?5:?hobby[4].checked=true;
??????????????????break;}????????
????case?6:?hobby[5].checked=true;
????????????????break;
?????default;
???????break;
????}
???????}
????????</script>
????</body>
</html>用switch完成最后一個任務,沒反應,求大神幫忙看一下,不勝感激?。?/p>
2016-07-28
全選和全不選那里i<hobby.length,不然你全不選的時候最后那項還是選中狀態。switch()那里其實沒有大問題,不過就是case 5的break后面多了個大括號,default后面是:寫成了;,以后寫代碼要小心了啊,可以把代碼放到自己的編輯器里看看。
2016-07-09
好吧,謝謝!
2016-07-09
不好意思,說錯了,我也不知道怎么回事?。?!
2016-07-09
你的switch里面的參數j沒有和文本框里的值聯系起來呀。var?j=parseInt(document.getElementById("wb").value);這個j是你用在函數checkone()里面的,你把var?j=parseInt(document.getElementById("wb").value);在switch前面再寫一遍試試。