2017-09-22 3 views
-1

を追加することはできませんプロパティを読み取ることができません。そのような単純なヌルの「価値」が、私はここで何をすべきさえ window.onload = function(){ヌルの「の値」プロパティを読み取って、配列内の

後に動作しないことは、私のコードは

window.onload = function(){ 
     var shashank = document.getElementById('shashank').value; 
     var kiran = document.getElementById('kiran').value; 
     var sumanth = document.getElementById('sumanth').value; 
     var arun = document.getElementById('arun').value; 

var arr = [shashank,kiran,sumanth,arun]; 

document.getElementById('btn').addEventListener('click', showName); 
} 
です

htmlのはこれです:あなたはこのようなのIdsをinlcudeするためにあなたのhtmlを更新する必要が

<html> 
    <head> 
     <title>Attendance Sheet</title> 
     <script src="app.js"></script> 
     <style> 
      #present{ 
       width:50px; 
       height:50px; 
      } 
     </style> 
    </head> 
    <body> 
     <select> 
      <option value="shashank">Shashank</option> 
      <option value="kiran">Kiran</option> 
      <option value="sumanth">Sumanth</option> 
      <option value="arun">Arun</option> 
     </select> 
      <button id="present">P</button> 
     <button id="btn">Submit</button> 
    </body> 
</html> 
+4

あなたの要素にはIDがありませんので、明らかに 'document.getElementById'は要素を選択しません。実際にここで何をしようとしていますか? – Turnip

答えて

0

あなたはのはMDN official documentation for getElementByID を見てみましょう、IDを持っていない要素を取得しようとしているので、それは単にですあなたがACCにしようとしているIDを持っていないあなたの<option>タグのほかにCodePen

<option id="shashank" value="shashank">Shashank</option> 
<option id="kiran" value="kiran">Kiran</option> 
<option id="sumanth" value="sumanth">Sumanth</option> 
<option id="arun" value="arun">Arun</option> 
0

<option id="shashank" value="shashank">Shashank</option>

か、次のようARRのオプションを取得することができます:

let options = document.getElementsByTagName("option"); 
0

に固定したサンプルを見てエッセー、私はあなたがしようとしているのは最適ではないと思います。選択肢が100個ある場合はどうなりますか?私はあなたの選択のためにeventListenerを作成し、選択されたオプションだけを得ることが最善の方法だと思います。

window.onload = function(){ 
    document.getElementById('btn').addEventListener('click', showName); 

    var myOpt = document.getElementById('mySelect').value; // myOpt = shashank 

    document.getElementById('mySelect').addEventListener('change', function(ev) {  
    // here you can save your selected option and do whatever you want 
    myOpt = ev.target.value 
    }); 
} 

<select>タグのid mySelectを追加することを忘れないでください!

<html> 
    <head> 
     <title>Attendance Sheet</title> 
     <script src="app.js"></script> 
     <style> 
      #present{ 
       width:50px; 
       height:50px; 
      } 
     </style> 
    </head> 
    <body> 
     <select id="mySelect"> 
      <option id="shashank" value="shashank">Shashank</option> 
      <option id="kiran" value="kiran">Kiran</option> 
      <option id="sumanth" value="sumanth">Sumanth</option> 
      <option id="arun" value="arun">Arun</option> 
     </select> 
     <button id="present">P</button> 
     <button id="btn">Submit</button> 
    </body> 
</html> 
関連する問題