Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将函数链接到JavaScript中的HTML按钮和文本字段_Javascript_Html_Css_Typescript - Fatal编程技术网

如何将函数链接到JavaScript中的HTML按钮和文本字段

如何将函数链接到JavaScript中的HTML按钮和文本字段,javascript,html,css,typescript,Javascript,Html,Css,Typescript,我被要求创建一个网页,用户插入年龄,并获得一些信息。如果用户未满21岁,请发送至disney.com。我不知道如何发送。到目前为止,我就是这么做的。如果我插入年龄并单击,我不确定函数是否正确,HTML按钮和文本字段不起作用 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>check is over 21

我被要求创建一个网页,用户插入年龄,并获得一些信息。如果用户未满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>

支票是否超过21英镑
函数CalAge(birthDateString){
var today=新日期();
var birthDate=新日期(birthDateString);
var age=today.getFullYear()-birthDate.getFullYear();
var month=today.getMonth()-birthDate.getMonth();
如果(月<0 | |(m==0&&today.getDate()=21){
警惕(“你已经超过21岁了!”);
} 
if(CalAge()<21){
警惕(“你不到21岁!”);
}


我相信您正在寻找重定向技术。完美地描述了如何使用JavaScript重定向到另一个链接。
如上所述,您可以使用以下两个选项中的任意一个重定向:

if(calAge() >= 21) {
    window.location.replace("http://www.disney.com");
}

您也可以使用
window.location.href
实现同样的功能。但是,我强烈建议您阅读上面提到的答案,以便更清楚地了解什么最适合您的情况。

以下是一些代码,可以帮助您

const ageInput=document.querySelector(“#age input”);
ageInput.valueAsDate=新日期();
document.querySelector(“#Bt Submit”).onclick=function()
{
让年龄=新日期(新日期()-ageInput.valueAsDate).getFullYear()-1970;
控制台日志(年龄);
如果(年龄<21){document.location.href=”https://disney.com" }
}
document.querySelector(“#Bt Reset”).onclick=function()
{
ageInput.valueAsDate=新日期();
}
出生日期:

提交年龄
重置年龄
如果用户输入的是他们的年龄(不是生日),则无需计算年龄。你可以这样做:

const btncalage=document.querySelector('btn calc age');
const btnReset=document.querySelector('btn reset');
const txtAge=document.querySelector(“#txt age”);
const MIN_AGE=21;
BTNCalPage.addEventListener('click',函数(e){
const age=parseInt(txtAge.value,10);
如果(年龄<最小年龄){
window.location.href=http://www.disney.com';
返回;
}
console.log('欢迎成人!');
});
btnReset.addEventListener('click',函数(e){
txtAge.value=“”;
});



您想知道如何将某人重定向到其他网站吗?是的,我还想知道,当我插入出生日期时,如果我的订单超过21,我可以收到消息。如果我还不到21岁,请转到disney.com。但是HTML文本字段和按钮不起作用。你能根据我的密码帮我吗?