在form表单中使用了button的onclick进行跳转,每次点击button时,就会引起表单的提交事件,解决方法就是在点击button的时候禁用表单提交事件:
<script type="text/javascript">
function check(){
var password = document.getElementById("password").value;
var pwd = document.getElementById("pwd").value;
if(password==pwd){
alert("验证正确");
document.getElementById('form1').submit(); //验证成功进行表单提交
}else{
alert("验证错误,请重新输入");
//window.location.href="index.jsp"; 可以将其连接到指定的位置
}
}
function add(){
$("#form1").submit(function(e){
e.preventDefault();
});
window.location.href = "index.jsp";
}
</script>
<form action="action" method="post" id="form1">
用 户 名:<input type="text" name="username" id="username">
密 码:<input type="password" name="password" id="password">
确认密码:<input type="password" name="pwd" id="pwd">
<input type="button" οnclick="add()" value="跳转"> /*这里点击跳转,没用,会刷新下页面*/
<input type="button" οnclick="check()" value="提交" >
</form>