2016-11-17 19 views
-3

チェックボックスをオンにすると、ビデオを表示するスクリプトを作成しました。document.getElementById( "x")。checkedは未定義を返します

HTML:

<p class="subParHeader" style="">Video or not</p> 
<form action="#" method="post" style="font-family: Arial;"> 
<input type="checkbox" title="Click here if you want to input a video" value="" id="FormVidOrNot"><input id="FormVidOrNotInput" value="" style="width: 30%"></input></input> 
</form> 

Javascriptを:

if(document.getElementById("FormVidOrNot").checked){ 

    document.write('<div class="div_video">'); 
    document.write(JSFormVidOrNotInput); 
    document.write('</div>'); 

} 

しかし、それはここで完璧に動作します本家私は、コードを実行するたびに、それは、undefinedを返します:

if(document.getElementById("FormVidOrNot").checked){ 
    var JSFormVidOrNotInput = document.getElementById("FormVidOrNotInput").value; 
} 

(この部分はあります最初のスクリプトよりもコードのほうが高い)

+0

'近いinputタグの必要は有効なHTMLではないではありません。この

<p class="subParHeader" style="">Video or not</p> <form action="#" method="post" style="font-family: Arial;"> <input type="checkbox" title="Click here if you want to input a video" value="Test" id="FormVidOrNot"> <input id="FormVidOrNotInput" value="" style="width: 30"> </form> 

注意してみてください。また、idはユニークである必要があります – brk

+0

user2181397それはコードの別の部分で行いました。完全に動作し、使用されたIDはHTMLで一度しか使用されません –

+0

user2181397が言ったように、ユニークなid。 – yashpandey

答えて

0

は、私たちの内側 `input` input`

document.getElementById("FormVidOrNot").addEventListener('click',function(){ 
    if(document.getElementById("FormVidOrNot").checked){ 
     var JSFormVidOrNotInput = document.getElementById("FormVidOrNotInput").value; 

     document.write('<div class="div_video">'); 
     document.write(JSFormVidOrNotInput); 
     document.write('</div>'); 
    } 

}); 
+0

答えをありがとう、それはまだ動作していません。 –

関連する問題