任務一二自己寫的,任務三參考別人的,怎么一個也實現不了呢???求助
<!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");
? ? ? ? if(var i=0;i<hobby.length;i++){
? ? ? ? ? ? if(hobby[i].type=="checkbox")
? ? ? ? ? ? {hobby[i].checked=true;}
? ? ? ? } ?
? ? ? ? ? // 任務1?
? ? ? ? ? ?
? ? ? ? }
? ? ? ? function clearall(){
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? if(var i=0;i<hobby.length;i++){
? ? ? ? ? ? ? ? hobby[i].checked=false;
? ? ? ? ? ? }
? ? ? ? ?// 任務2 ? ?
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? ? ? function checkone()
? ? ? ? {
? ? ? ? ? ??
? ? ? ? ? ? var j=document.getElementById("wb").value;
? ? ? ? ? ? var hobby=document.getElementById("hobby"+"j");
? ? ? ? ?// 任務3
? ? ? ? ? ? hobby.checked=true;
? ? ? ? }
? ? ? ? </script>
? ? </body>
</html>
2016-09-06
你需要弄清楚for循環和if判斷語句的格式
for(三個判斷語句){}
????????if(一個判斷條件){}
*所以你任務1,2錯在把for循環的判斷語句寫出了if
2.任務3中【var hobby=document.getElementById("hobby"+"j");】這一句需要去掉j的雙引號
【“j”】這個意思是hobby字符串加上j字符串,查找的Id就是hobbyj,但是是沒有這個名字的id的
【j】這樣子就相當于一個變量,而j的值在上一句中通過“wb”id的查找出來的一個數字。
*任務3的正確寫法【var hobby=document.getElementById("hobby"+j);】
2016-09-23
補充任務三
?function checkone(){
? ? ? ? ? ? var j=parseInt(document.getElementById("wb").value);
? ? ? ? ? ? if(j>0&&j<7){
? ? ? ? ? ? ? ? var hobby = document.getElementById("hobby"+j);
? ? ? ? ? ? ? ? hobby.checked=true;
? ? ? ? ? ? }else{
? ? ? ? ? ? alert("長眼睛出氣的!");} ? ?
? ? ? ? }