我查看了最近关于 onsubmit 事件未触发该方法的博客。这些建议对这个问题没有帮助。我已经尝试过这种方法并在另一个 html 页面中形成,但也没有用。所以我无法找出主要问题在哪里?我的代码:
<script>
function validateLogin()
{
var nameCheck=document.Log.username.value;
var passwordCheck=document.Log.password.value;
var status=false;
if(nameCheck.length<4)
{
document.Log.getElementById("nameLoc").innerHTML=
'Put your Email Address for this Community\'s Sake';
status=false;
}
else
{
document.Log.getElementById("nameLoc").innerHTML=
'Naming Convention Allright';
status=true;
}
if(passwordCheck.length<8)
{
document.Log.getElementById("passwordLoc").innerHTML=
'Password Does not Meet with Standard';
status=false;
}
else
{
document.Log.getElementById("passwordLoc").innerHTML=
'Passowrd Convention Allright';
status=true;
}
return status;
}
</script>
<form name="Log" method="post" action="login.php" onsubmit="return
validateLogin()">
<div class="imgcontainer">
<img src="login.png" alt="Avatar" class="avatar">
</div>
<div class="container">
<label for="username"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username"
required><span id="nameLoc"></span>
<br>
<label for="password"><b>Password</b></label><span id="passwordLoc">
</span>
<input type="password" placeholder="Enter Password" name="password"
required><span id="passwordLoc"></span>
<br>
<button type="submit">Login</button>
<label>
<input type="checkbox" checked="checked" name="remember">
Remember me
</label>
</div>
</form>
</div>
最佳答案
您正在尝试在 form
上使用 getElementById
- 但它是 document
的函数。
更改为:
if (nameCheck.length < 4) {
document.getElementById("nameLoc").innerHTML =
'Put your Email Address for this Community\'s Sake';
status = false;
}
else {
document.getElementById("nameLoc").innerHTML =
'Naming Convention Allright';
status = true;
}
if (passwordCheck.length < 8) {
document.getElementById("passwordLoc").innerHTML =
'Password Does not Meet with Standard';
status = false;
}
else {
document.getElementById("passwordLoc").innerHTML =
'Passowrd Convention Allright';
status = true;
}
关于javascript - onsubmit 不调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49339818/