牛骨文教育服务平台(让学习变的简单)
博文笔记

JQuery禁用form提交事件

创建时间:2017-06-04 投稿人: 浏览次数:2276

在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" onclick="add()" value="跳转">       /*这里点击跳转,没用,会刷新下页面*/
    <input type="button" onclick="check()" value="提交" > 
</form>


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。