2014-06-20 2 views
0

올해 일하는 변수를 얻을 수없는 것, 나는 이것을 시도하고 작동합니다. 나는 JS와 HTML을 처음 사용합니다. 순진한 질문을 용서하십시오.HTML에서 자바 스크립트 변수를 사용할 수없는 것 같습니다

간단한 양식을 포함한 HTML 페이지. 방문객에게 자신의 이름을 입력하도록 요청하고, 그/그녀는 태어났습니다. 제출 버튼을 클릭하면 양식에서 데이터를 추출하는 JavaScript를 작성하십시오. 스크립트는 새로운 문서에 다음 정보를 표시해야합니다. 안녕하세요, 방문자 이름, xx 세입니다.

<!DOCTYPE html> 
<html> 
    <body> 

<form action="demo_form.asp"> 
    First name: <input type="text" id="FirstName" ><br> 
Birth year: <input type="integer" id="BirthYear" ><br> 
<input type="button" value="Submit" onclick = "test1();"> 
</form> 

    <script> 
    var curYear = today.getFullYear(); 

    function test1(){ 

var yy = document.getElementById("FirstName").value; 
var xx = document.getElementById("BirthYear").value; 

if(xx < curYear){ 
xx = curYear - xx; 
    document.write("Hello," + yy + ", you are " + xx + " years old"); 

} 
else{ 
alert("Your age is invalid"); 
} 
} 



    </script> 


    </body> 
    </html> 

답변

1

당신은 변수 today를 인스턴스화해야합니다. var curYear = today.getFullYear();

앞에 var today = new Date();을 추가하십시오.
1

당신은 today을 인스턴스화하지 않은 (. 미래 년에 방문자 유형이 경우 스크립트 오류 메시지가 표시되어야합니다) :

var today = new Date();    // add this line 
var curYear = today.getFullYear(); // now `today` will resolve 
관련 문제