我正在使用一个表单,在该表单中我获取用户输入的密码,然后 javascript 检查密码是否匹配。但即使需要密码,如果输入框留空,表单也会被提交。我正在使用 javascript 提交表单。虽然我有解决问题的方法,但我想问为什么会发生这种情况,而不是正常的操作Please fill in this field
,通常在这样做时出现。
HTML-
<form action="check.php" method="POST" id="userform">
USERNAME:<input name="uname" type="text" required/>
PASSWORD:<input type="password" id="upass" name="upass" type="text" required/>
RETYPE PASSWORD:<input type="password" id="reupass" name="reupass" type="text" required/>
<input type="button" value="Submit" onclick="passCheck()">
</form>
Run Code Online (Sandbox Code Playgroud)
JavaScript-
function passCheck(){
var pass1 = document.getElementById('upass').value;
var pass2 = document.getElementById('reupass').value;
if(pass1 == pass2){
document.getElementById('userform').submit();
}
else{
alert("Both password inputs do not match. Please retry.");
document.getElementById('userform').reset();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑-我希望当我单击按钮并且密码字段留空时应该出现此内容: https: //i.stack.imgur.com/cGuCK.jpg
当我在IE中运行我的网页时,我收到此错误(代码在其他浏览器中运行正常).
这是HTML代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<--- some html code --->
<iframe src='http://localhost/page1.php' style="background: transparent; overflow: hidden; height: 210px; width: 390px;" frameborder="0" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是page1.php
<!DOCTYPE html>
<html>
<body>
<form action="usercheck.php" method="POST">
USERNAME:<input name="uname" id="uname" type="text" maxlength="12" required/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是usercheck.php
<?php
//some php code
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<--- some html code --->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
问题是当我usercheck.php
点击提交按钮后到达page1.php
我得到的错误http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js. Access is …