我被要求创建一个网页,用户可以输入年龄并获得一些信息。如果用户未满21岁,请发送至disney.com。我不知道如何发送。到目前为止,这是我所做的。我不确定该功能是否正确,如果插入年龄并单击,HTML按钮和文本字段将无法正常工作。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>check is over 21 or not</title>
<script>
function CalAge(birthDateString) {
var today = new Date();
var birthDate = new Date(birthDateString);
var age = today.getFullYear() - birthDate.getFullYear();
var month = today.getMonth() - birthDate.getMonth();
if (month < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
if(CalAge() >= 21) {
alert("You are over 21!");
}
if(CalAge() < 21) {
alert("You are under 21!");
}
</script>
</head>
<body >
<input type="text" name="age" size="30" onClick="Calage()" placeholder="Enter your age" />
<br />
<input type="button" onclick="CalAge()" value="SUBMIT Age">
<input type="reset" onclick="CalAge()" value="Reset age">
</html>
最佳答案
这是一些代码可以帮助您...
const ageInput = document.querySelector('#age-input');
ageInput.valueAsDate = new Date();
document.querySelector('#Bt-Submit').onclick = function()
{
let Age = new Date( new Date() - ageInput.valueAsDate ).getFullYear() - 1970;
console.log(Age);
if ( Age < 21 ) { document.location.href = "https://disney.com" }
}
document.querySelector('#Bt-Reset').onclick = function()
{
ageInput.valueAsDate = new Date();
}
<label>birth date :
<input type="date" id="age-input" size="30" />
</label>
<br />
<button id="Bt-Submit">SUBMIT Age</button>
<button id="Bt-Reset">Reset Age</button>
关于javascript - 如何在JavaScript中将函数链接到HTML按钮和文本字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54759038/