[英]Javascript in html, variable doesn't seem to be usable
I can't seem to get the variable for year working,I tried this and it should work. 我似乎无法获得工作年限的变量,我尝试了一下,它应该可以工作。 I am new to JS and HTML please forgive the naive questions
我是JS和HTML的新手,请原谅天真的问题
HTML page that includes a simple form. 包含简单表单的HTML页面。 ask the visitor to type in his/her name, and year he/she was born.
请访客输入他/她的名字,以及他/她的出生年份。 Write a JavaScript to extract the data from the form when the submit button is clicked.
单击提交按钮时,编写JavaScript以从表单中提取数据。 The script should display the following information in a new document.
该脚本应在新文档中显示以下信息。 Hello, Visitor's name, you are xx years old.
您好,访客名字,您xx岁。 (Your script should show an error message if a visitor type in a future year.)
(如果访客在来年键入,您的脚本应显示一条错误消息。)
<!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>
You haven't instantiated today
: 您
today
尚未实例化:
var today = new Date(); // add this line
var curYear = today.getFullYear(); // now `today` will resolve
You need to instantiate the variable today
. 您
today
需要实例化变量。 Add var today = new Date();
var today = new Date();
添加var today = new Date();
before the var curYear = today.getFullYear();
在
var curYear = today.getFullYear();
之前var curYear = today.getFullYear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.