因此,我有这样的形式:
<form name="login" id="login" action="" method="POST" onSubmit="return test()">
<input type="text" size="10" name="username" /><div id="wrongUser"></div>
<br />
<input type="password" size="10" name="password" /><div id="wrongPass"></div>
<br />
<input type="submit" value="submit" name="submit" /><br /><br />
</form>
这两项职能:
function test()
{
var user = document.login.username.value;
var pass = document.login.password.value;
if((user == "" || user == null) && (pass == "" || pass == null))
{
document.getElementById( wrongUser ).innerText = "Please Enter Username";
document.getElementById( wrongPass ).innerText = "Please Enter Password";
return false;
}
if(checkEmpty(user, wrongUser , "Please Enter Username"))
return false
if(checkEmpty(pass, wrongPass , "Please Enter Password"))
return false;
return true;
}
function checkEmpty(name, id, output)
{
if(name == "" || name == null)
{
document.getElementById(id).innerText = "";
document.getElementById(id).innerText = output;
return true;
}
else
return false;
}
Now the functions kinda work but not how I would think. If the user only doesn t enter anything (first time) then they get the 2 errors. If the user enter just a username (second time) then the username error should disappear, but it doesn t. If I take it out of the function and replace the variables with their normal values then it works just fine. Why would it change when put in this function?