-1

当我单击提交按钮时,它只是加载下一页而不是运行 javascript 代码。

这是我的 JavaScript 代码:

function Validate(thisForm)
    {
        alert("started");
        var username22 = document.thisForm.getElementById('username');
        var password22 = document.thisForm.getElementById('password');
        var password222 = document.thisForm.getElementById('password2');
        var email22 = document.thisForm.getElementById('email');
        var email222 = document.thisForm.getElementById('email2');
        var legalcheck22 = document.thisForm.getElementById('legal');
        alert("started 2");
        if(username22.value == "")
        {
            alert("Please enter a username.");
            focus.username22;
            return false;
        }
        alert("started 3");
        if(password22.value.length < 5)
        {
            alert("Password needs to be at least 5 characters long.");
            focus.password22;
            return false;
        }
        alert("started 4");
        if(password22.value != password2.value)
        {
            alert("Passwords don't match.");
            focus.password22;
            return false;
        }
        alert("started 5");
        if(email22.value == "")
        {
            alert("Please enter an email.");
            focus.email22;
            return false;
        }
        alert("started 6");
        if(email222.value == "")
        {
            alert("Please confirm your email.");
            focus.email222;
            return false;
        }
        alert("started 7");
        if(email22.value != email222.value)
        {
            alert("Emails don't match.");
            focus.email22;
            return false;
        }
        if(legalcheck22.checked == false)
        {
            alert("You need to agree to our Terms and Conditions.");
            focus.legalcheck22;
            return false;
        }
        alert("pass");
        return true;
    }

这是我的表格

<div class="formLayout">
        <form id="registerForm" name="registerForm" method="post" onsubmit="return Validate(registerForm)" action="register.php" >
          <h3 class="centeredText">Register Account</h3>
          <p>
            <label>Username</label>
            <input name="username" id="username" />
            <br />
            <label>Password</label>
            <input name="password" type="password" id="password" />
            <br />
            <label>Confirm Passwrd</label>
            <input name="password2" type="password" id="password2" />
            <br />
            <label>Email</label>
            <input name="email" id="email" />
            <br />
            <label>Confirm Email</label>
            <input name="email2" id="email2" />
            <br />
            <label>Captcha</label>
            <input id="captcha" />
          </p>
<p>&nbsp;</p>
          <div id="centerButtons">
            <input type="submit" name="register" id="register" value="Register" />
            <input type="reset" name="register2" id="register2" value="Reset" />
            <p>&nbsp;</p>
          </div> 
          <!-- end of centerButtons-->
          <p>
            <input type="checkbox" name="legal" id="legal" />
            <label for="legal">I agree to the <a href="terms.html">Terms and Conditions</a>.</label>
          </p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
</form>
    </div> 
    <!-- end of formLayout div-->

我不确定为什么在启动 javascript 代码时页面会继续加载。它设法显示第一个警报(“已启动”),然后重新加载页面。你能帮帮我吗?谢谢

4

1 回答 1

1

getElementByIddocument对象的属性,而不是所有Element对象的属性。

var username22 = document.thisForm.getElementById('username');将抛出异常,杀死 JS,并允许表单提交正常继续。

于 2013-04-29T20:59:35.153 回答