-1

我创建了一个登录页面,其中包含一个登录表单,该表单将用户名和密码提交给 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>

拜托,谁能指导我。提前感谢

4

3 回答 3

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;
        }
    }
于 2012-08-29T07:10:02.633 回答
1

继承人的问题.. 这个函数只返回 FALSE。没有返回 true 声明.. 对于要提交的表单,“onsubmit”应该返回 true :)

于 2012-08-29T07:12:35.487 回答
1

你formValidator()总是返回false,如果onsubmit返回false,就不会提交。如果一切都有效,请务必返回 true。

于 2012-08-29T07:10:10.003 回答