課程
/前端開發
/JavaScript
/JavaScript進階篇
"hobby"+j是什么意思?
2016-02-23
源自:JavaScript進階篇 9-4
正在回答
"hobby"是一個字符串,j是一個數值,
"hobby"+j 就成了某個input的id值
j是框里的值,hobby+j 比如框里是1,則hobby+j就等于hobby1
?不知道你在問什么嘞。。給你看看我寫的代碼好了
<!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");
? ? ? ? ? ?for(i=0;i<hobby.length;i++){
? ? ? ? ? ?if(hobby[i].type=="checkbox")
? ? ? ? ? ?hobby[i].checked=true;
? ? ? ? ? ?}
? ? ? ? ? // 任務1?
? ? ? ? ? ?
? ? ? ? }
? ? ? ? function clearall(){
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? for(i=0;i<hobby.length;i++){
? ? ? ? ? ? hobby[i].checked=false;
? ? ? ? ? ? }
? ? ? ? ?// 任務2 ? ?
? ? ? ? ? ??
? ? ? ??
? ? ? ? function checkone(){
? ? ? ? ? ? var j=document.getElementById("wb").value;
? ? ? ? ? ? hobby[j-1].checked=true;
? ? ? ? ?// 任務3
? ? ? ? </script>
? ? </body>
</html>
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態交互世界
2 回答hobby[j-1]
1 回答var hobby = document.getElementById("hobby"+j);
2 回答var hobby = document.getElementById("hobby"+j);
1 回答var hobby = document.getElementById("hobby"+j)為什么要加j
1 回答("hobby"+j)為什么不是("hobby+j")或者("'hobby'+j")?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-03-03
"hobby"是一個字符串,j是一個數值,
"hobby"+j 就成了某個input的id值
2016-02-24
j是框里的值,hobby+j 比如框里是1,則hobby+j就等于hobby1
2016-02-23
?不知道你在問什么嘞。。給你看看我寫的代碼好了
<!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");
? ? ? ? ? ?for(i=0;i<hobby.length;i++){
? ? ? ? ? ?if(hobby[i].type=="checkbox")
? ? ? ? ? ?hobby[i].checked=true;
? ? ? ? ? ?}
? ? ? ? ? // 任務1?
? ? ? ? ? ?
? ? ? ? }
? ? ? ? function clearall(){
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? for(i=0;i<hobby.length;i++){
? ? ? ? ? ? hobby[i].checked=false;
? ? ? ? ? ? }
? ? ? ? ?// 任務2 ? ?
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? ? ? function checkone(){
? ? ? ? ? ? var j=document.getElementById("wb").value;
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? hobby[j-1].checked=true;
? ? ? ? ?// 任務3
? ? ? ??
? ? ? ? }
? ? ? ??
? ? ? ? </script>
? ? </body>
</html>