简体   繁体   English

JavaScript按钮不起作用

[英]JavaScript button doesn't work

I am very new to using JavaScript so bear with me. 我是使用JavaScript的新手,所以请耐心等待。 I only finished the different courses found on Codecademy so far. 到目前为止,我只完成了Codecademy上发现的不同课程。

So I'm trying to make a sign up form for a website where you need a user. 因此,我正在尝试为您需要用户的网站制作一个注册表单。 I found a pretty cool one on the internet that I changed a bit and also made use of the Datepicker from jQuery. 我在互联网上发现了一个非常酷的,我改变了一点,并使用了jQuery中的Datepicker。 It looks like this: 它看起来像这样:

注册表格

I got code to check whether the user filled out the different fields but it doesn't seem to work. 我有代码来检查用户是否填写了不同的字段,但它似乎不起作用。 In my index.html file I included the following file: 在我的index.html文件中,我包含以下文件:

<script src="js/signupsubmit.js"></script>

And at the bottom of the form I do this: 在表格的底部我这样做:

<div>
    <p id="sign_user" onClick="Submit()">Sign Up</p>
</div>

So good so far. 到目前为止这么好。 In the JavaScript I have the following code: 在JavaScript中我有以下代码:

function Submit() {
    var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]{2,5}$/;
    var fname = document.form.Name.value,
        lname = document.form.LastName.value,
        femail = document.form.Email.value,
        freemail = document.form.enterEmail.value,
        fpassword = document.form.Password.value,
        dateObject = $("#datepicker").datepicker(
        {
            onSelect: function()
            {
                var dateObject = $(this).datepicker('getDate');
            }
        });
    if( fname == "") {
        document.form.name.focus();
        document.getElementById("errorBox").innerHTML = "Enter First Name";
        return false;
    }
    if( lname == "" )
    {
        document.form.LastName.focus() ;
        document.getElementById("errorBox").innerHTML = "Enter Last Name";
        return false;
    }

    if (femail == "" )
    {
        document.form.Email.focus();
        document.getElementById("errorBox").innerHTML = "Enter your Email Address";
        return false;
    }else if(!emailRegex.test(femail)){
        document.form.Email.focus();
        document.getElementById("errorBox").innerHTML = "Enter a Valid Email Address";
        return false;
    }

    if (freemail == "" )
    {
        document.form.enterEmail.focus();
        document.getElementById("errorBox").innerHTML = "Re-enter the Email Address";
        return false;
    }else if(!emailRegex.test(freemail)){
        document.form.enterEmail.focus();
        document.getElementById("errorBox").innerHTML = "Re-enter a Valid Email Address";
        return false;
    }

    if(freemail !=  femail){
        document.form.enterEmail.focus();
        document.getElementById("errorBox").innerHTML = "The Email Addresses don't Match!";
        return false;
    }


    if(fpassword == "")
    {
        document.form.Password.focus();
        document.getElementById("errorBox").innerHTML = "Enter a Password";
        return false;
    }

    if(dateObject == null) {
        document.form.datepicker.focus();
        document.getElementById("errorBox").innerHTML = "Please Enter a Birthday";
    }
}

I am a bit shaky on trying to read the Datepicker too. 我试图阅读Datepicker也有点不稳定。 But otherwise, can you possibly spot what might be missing in this puzzle? 但除此之外,你能否发现这个谜题中可能缺少的东西? When I press the blue "Sign Up" button, literally nothing happens. 当我按下蓝色的“注册”按钮时,几乎没有任何反应。 I can provide more info if needed. 如果需要,我可以提供更多信息。

You have a mistake addressing the form: The correct formula is not document.form , but document.forms[0] . 您在处理表单时遇到错误:正确的公式不是document.form ,而是document.forms[0] Or even better, I recommend you to give the form a specific unique name, and address it by that name: 或者甚至更好,我建议您为表单指定一个特定的唯一名称,并使用该名称对其进行处理:

HTML: HTML:

<form name="mydata">...</form>

Javascript: 使用Javascript:

var fm=document.forms.mydata

Also, take care of lowercase/uppercase lettering: Identifiers in Javascript are case-sensitive , which means that the input "Name" must be always addressed as "Name" (you mispelled that identifier in the line document.form.name.focus() ). 此外,照顾小写/大写字母的:在Javascript标识符是大小写敏感的 ,这意味着输入“姓名”必须始终称呼为“名”(你拼写错误的行标识符document.form.name.focus() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM