使用javascript后,“提交”按钮无效

时间:2012-08-29 07:07:54

标签: php javascript

我创建了一个登录页面,其中包含一个登录表单,用于向loginproc.php提交用户名和密码,以便检查其有效性并且一切正常。无论如何,在登录页面上我编写了一个JavaScript函数,在用户名和密码字段为空时提醒用户。在我完成之后,JavaScript工作正常,但是当我点击提交按钮时,绝对没有任何事情发生!

----------------------------------------这是JavaScript --- -------------------

<script type='text/javascript'>

function formValidator(){
    // Make quick references to our fields
    var username = document.getElementById('username');
    var password = document.getElementById('password');


    // Check each input in the order that it appears in the form!
    if(notEmpty(username, "The username field is empty!")){
        if(notEmpty(password, "The password is empty!")){

        }
    }

    return false;

}

function notEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return false;
    }
    return true;
}
</script>

----------------------------------------这是表格--- -------------------

<form method="post" action="loginproc.php" onsubmit="return formValidator()" >


<tr><td colspan="3"><img src="icons/login.jpg" alt="Edugate" width="366" height="123"></td></tr>
<tr><td colspan="3" nowrap bgcolor="#990033" class="infoTitle"><div align="center"></div></td></tr>
<tr><td colspan="3" nowrap class="infoTitle"><div align="left">user login </div></td></tr>
<tr><td colspan="3" nowrap bgcolor="#990033" class="infoTitle"><div align="center"></div></td></tr>
<tr><td width="58">Username</td>
<td width="4">:</td>
<td width="297"><input type="text" id="username" name="username" size="20"></td></tr>
<tr><td>Password</td><td>:</td><td><input type="password" id="password" name="password" size="20"></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" class="BtnInTable" value="Login"></td></tr>
<?php if ($error == 1)
print "<tr><td colspan='3' class='ppos'><img src='icons/error.png' alt='error'> Icorrect usename or password...</td></tr>";?>
<tr><td colspan="3" nowrap bgcolor="#990033" class="infoTitle"><div align="center"></div></td></tr>
</form>

请说,任何人都可以指导我。谢谢你的推荐

3 个答案:

答案 0 :(得分:2)

您忘了return true;,否则会转到return false;

    // Check each input in the order that it appears in the form!
    if(notEmpty(username, "The username field is empty!")){
        if(notEmpty(password, "The password is empty!")){
                return true;
        }
    }

答案 1 :(得分:1)

formValidator()始终返回false,如果向onsubmit返回false,则不会提交。 如果一切都有效,请务必返回true。

答案 2 :(得分:1)

继承人的问题.. 此函数仅返回FALSE。 没有回报真实的陈述.. 要提交表单,“onsubmit”应该返回true:)