2016-03-07 150 views
-1

那是我的代碼 我正在嘗試製作一個評分系統。 Gusenbauer應該是學生和1ahitm陣列的地方,例如他的班級 我來自java,這些數組在這裏混淆了我。 我需要做些什麼才能做出像noten [pupil] [class] = grade;可能? 另外我的document.notenformular.name.value是未定義的,任何想法爲什麼?初始化二維數組的問題

var noten = new Array(); 
    noten["Gusenbauer"] = new Array(); 
    noten["Gusenbauer"]["1ahitm"] = 0; 
    noten["seppl"]["1ahitm"] = new Array(); 
    noten["seppl"]["1ahitm"] = 0; 
    noten["leo"]["1ahitm"] = new Array(); 
    noten["leo"]["1ahitm"] = 0; 
    noten["Gussenbauer"]["2ahitm"] = new Array(); 
    noten["Gussenbauer"]["2ahitm"] = 0; 
    noten["steiner"]["2ahitm"] = new Array(); 
    noten["steiner"]["2ahitm"] = 0; 
    noten["roland"]["2ahitm"] = new Array(); 
    noten["roland"]["2ahitm"] = 0; 
    alert(noten["roland"]["2ahitm"]); 
    alert("was"); 
function noteEintragen() { 
     var name = document.notenformular.name.value; 
     alert(name); 
     var note = document.notenformular.note.value; 
     var klasse = document.notenformular.klasse.value; 

     noten[name][klasse] = note; 


    } 
ody> 
<div id="wrapper"> 

    <h1>Name - Noteneintragung - Klassenspiegel</h1> 
    <hr> 
    <p>Klasse auswählen, Name auswählen, Note eintragen</p> 
    <form name="notenformular"> 
     Klasse 
     <br> 
     <select name="klasse" onchange="java_script_:show(this.options[this.selectedIndex].value)" size="20" tabindex="2"> 
      <option value="1ahitm">1ahitm</option> 
      <option value="2ahitm">1bhitm</option> 

     </select> 
     <br> 
     <br> Name 
     <br> 
     <div id="hiddenDiv" style="display:none"> 
      <select name="name" id="test" size="20" tabindex="1"> 
       <option value="Gusenbauer">Gusenbauer</option> 
       <option value="seppl">seppl</option> 
       <option value="leo">leo</option> 
      </select> 

回答

1

只需使用JavaScript對象來存儲你的價值觀

noten = { 
    "Gusenbauer" :{ 
     "1ahitm" : 0, 
     "2ahitm": 0 
    }, 
    "seppl" :{ 
     "1ahitm" : 0 
    }, 
    "steiner" :{ 
     "2ahitm" : 0 
    }, 
    "roland" :{ 
     "2ahitm" : 0 
    } 
}; 

現在你可以使用方括號從Noten的訪問對象的值。

noten [「roland」] [「2ahitm」]; // 0

+0

好的,但是,如果你再往下看,我可以怎麼看[pupil] [class] = note?你有什麼想法爲什麼note.value是未定義的。我還可以在循環中使用嗎?因爲我需要訪問它們來打印它們例如編輯:哦,謝謝你的方式:) –

+0

你需要在兩個語句中做到這一點。 noten [pupil] = {}; noten [pupil] [class] = note; – Darshan

+0

但是爲什麼?爲什麼我需要空{}我很抱歉,這對我來說有點複雜。所以如果我想把對象增加到3,那麼它會被記住[pupil] = {}; noten [subject] = {}; noten [pupil] [class] [subject] = 2; –