input button实现跟submit一样的回车提交(使用onsubmit时js跳转失效)

15 篇文章 0 订阅

最开始表单部分用

	<form action="" method="get" οnsubmit="return ajaxLee()">
		<span>用 户 名:</span><input type="text" name="username" id="username" /><br/>
		<span>密    码:</span><input type="password" name="password" id="password" /><br/>
		<span>验 证 码:</span><input type="verify" name="verify" id="verify"/><br/>
		<input type="submit" name="submit" value="提交"/>
		<input type="reset" name="reset" value="重置" />
	</form>
js部分用的
function ajaxLee(){	
	var password = document.getElementById('password').value;
	var verify = document.getElementById('verify').value;
	var urls = "http://localhost/addressBook/index.php";	
		
	$.ajax({ 
		type: "get", 
		url: urls, 
		data: "action=login&func=verify&name="+username+"&pwd="+password, //用来传递参数pageval、uid
		dataType:'text',				//使用jsonp
		async:false,
		cache:false,
		success: function (msg) { 
			switch(msg){
			case 'right':
				alert('输入信息正确!欧耶!');
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">				</span>	</span><span style="font-family: Arial, Helvetica, sans-serif;">			</span><span style="font-family: Arial, Helvetica, sans-serif;">location.href="http://www.baidu.com/";</span>
				break;
			case 'error':
				alert('密码错误!');
				break;
			case 'noexist':
				alert('用户名不存在!');
				break;
			case 'failed':
				alert('查询用户名失败!');
				break;
			default:
				alert('返回结果有误!错误信息:'+msg);
			}//end switch
		}, 
		error: function (XMLHttpRequest, textStatus, errorThrown) {     
		    alert("XMLHttpRequest.readyState::"+XMLHttpRequest.readyState);		
		}
	}); //end ajax  

}//end func ajaxLee

这种形式进行表单验证提交,但是在js中的location.href="http://www.baidu.com/"却不能实现跳转,其实这个跳转功能是正常的,出现问题的部分是onsubmit事件是对表单提交进行验证的事件。解决方法,将<input type="submit">改成<input type="button">。

具体如下:

html部分

<form action="" method="get" >
			<span>用 户 名:</span><input type="text" name="username" id="username" /><br/>
			<span>密    码:</span><input type="password" name="password" id="password" /><br/>
			<span>验 证 码:</span><input type="verify" name="verify" id="verify"/><br/>
			<input type="button" name="submit" value="提交"/>
			<input type="reset" name="reset" value="重置" />
		</form>

js部分

function ajaxLee(){
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var verify = document.getElementById('verify').value;

	var urls = "index.php";	
	$.ajax({ 
		type: "get", 
		url: urls, 
		data: "action=login&func=verify&name="+username+"&pwd="+password, //用来传递参数pageval、uid
		dataType:'text',				
		async:false,
		cache:false,
		success: function (msg) { 
			switch(msg){
			case 'right':
				alert('输入信息正确!欧耶!');
				window.location.href='http://www.baidu.com/';
				break;
			case 'error':
				alert('密码错误!');
				break;
			case 'noexist':
				alert('用户名不存在!');
				break;
			case 'failed':
				alert('查询用户名失败!');
				break;
			default:
				alert('返回结果有误!错误信息:'+msg);
			}//end switch
		}, 
		error: function (XMLHttpRequest, textStatus, errorThrown) {     
		    alert("ajax error!");		
		}
	}); //end ajax  

}//end func ajaxLee


改成submit之后可以进行正常跳转了,但是却不能实现回车提交功能了,然后添加回车事件即可:

html部分

<form action="" method="get" >
			<span>用 户 名:</span><input type="text" name="username" id="username" /><br/>
			<span>密    码:</span><input type="password" name="password" id="password" /><br/>
			<span>验 证 码:</span><input type="verify" name="verify" id="verify" οnkeydοwn="javascript:butOnClick();"/><br/>
			<input type="button" name="submit" value="提交" id="bsubmit" οnclick="ajaxLee()"/>
			<input type="reset" name="reset" value="重置" />
		</form>

js部分

function ajaxLee(){
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var verify = document.getElementById('verify').value;

	var urls = "index.php";	
	$.ajax({ 
		type: "get", 
		url: urls, 
		//data:{mod:"db",name:"leedaning30",pwd:"123456",email:"2687986030",sta:"register"},
		data: "action=login&func=verify&name="+username+"&pwd="+password, //用来传递参数pageval、uid
		dataType:'text',				
		async:false,
		cache:false,
		success: function (msg) { 
			switch(msg){
			case 'right':
				alert('输入信息正确!欧耶!');
				window.location.href='http://www.baidu.com/';
				break;
			case 'error':
				alert('密码错误!');
				break;
			case 'noexist':
				alert('用户名不存在!');
				break;
			case 'failed':
				alert('查询用户名失败!');
				break;
			default:
				alert('返回结果有误!错误信息:'+msg);
			}//end switch
		}, 
		error: function (XMLHttpRequest, textStatus, errorThrown) {     
		    alert("ajax error!");		
		}
	}); //end ajax  

}//end func ajaxLee

function butOnClick() { 
	if (event.keyCode == 13) { 
		var button = document.getElementById("bsubmit"); //bsubmit 为botton按钮的id 
		button.click(); 
		return false; 
	}//end if()
}//end func butOnClick()







	//doucment.form.action="http://www.baidu.com/";
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值