課程
/前端開發(fā)
/JavaScript
/JavaScript進(jìn)階篇
"hobby"+j是什么意思?
2016-02-23
源自:JavaScript進(jìn)階篇 9-4
正在回答
"hobby"是一個(gè)字符串,j是一個(gè)數(shù)值,
"hobby"+j 就成了某個(gè)input的id值
j是框里的值,hobby+j 比如框里是1,則hobby+j就等于hobby1
?不知道你在問什么嘞。。給你看看我寫的代碼好了
<!DOCTYPE HTML>
<html>
? ? <head>
? ? ? ? <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
? ? ? ? <title>無標(biāo)題文檔</title>
? ? </head>
? ??
? ? <body>
? ? ? ? <form>
? ? ? ? ? 請(qǐng)選擇你愛好:<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>請(qǐng)輸入您要選擇愛好的序號(hào),序號(hào)為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;
? ? ? ? ? ?}
? ? ? ? ? // 任務(wù)1?
? ? ? ? ? ?
? ? ? ? }
? ? ? ? function clearall(){
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? for(i=0;i<hobby.length;i++){
? ? ? ? ? ? hobby[i].checked=false;
? ? ? ? ? ? }
? ? ? ? ?// 任務(wù)2 ? ?
? ? ? ? ? ??
? ? ? ??
? ? ? ? function checkone(){
? ? ? ? ? ? var j=document.getElementById("wb").value;
? ? ? ? ? ? hobby[j-1].checked=true;
? ? ? ? ?// 任務(wù)3
? ? ? ? </script>
? ? </body>
</html>
舉報(bào)
本課程從如何插入JS代碼開始,帶您進(jìn)入網(wǎng)頁動(dòng)態(tài)交互世界
2 回答hobby[j-1]
1 回答var hobby = document.getElementById("hobby"+j);
2 回答var hobby = document.getElementById("hobby"+j);
1 回答("hobby"+j)為什么不是("hobby+j")或者("'hobby'+j")?
1 回答var hobby = document.getElementById("hobby"+j)為什么要加j
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-03-03
"hobby"是一個(gè)字符串,j是一個(gè)數(shù)值,
"hobby"+j 就成了某個(gè)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>無標(biāo)題文檔</title>
? ? </head>
? ??
? ? <body>
? ? ? ? <form>
? ? ? ? ? 請(qǐng)選擇你愛好:<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>請(qǐng)輸入您要選擇愛好的序號(hào),序號(hào)為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;
? ? ? ? ? ?}
? ? ? ? ? // 任務(wù)1?
? ? ? ? ? ?
? ? ? ? }
? ? ? ? function clearall(){
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? for(i=0;i<hobby.length;i++){
? ? ? ? ? ? hobby[i].checked=false;
? ? ? ? ? ? }
? ? ? ? ?// 任務(wù)2 ? ?
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? ? ? function checkone(){
? ? ? ? ? ? var j=document.getElementById("wb").value;
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? hobby[j-1].checked=true;
? ? ? ? ?// 任務(wù)3
? ? ? ??
? ? ? ? }
? ? ? ??
? ? ? ? </script>
? ? </body>
</html>