實現單擊或者回車雙重登錄動作(頁面登錄的回車事件onkeydown)

<script type="text/javascript">
//基於jquery的button默認enter事件(回車事件)
document.onkeydown = function (e) { 
	var theEvent = window.event || e;//瀏覽器兼容性 
	var code = theEvent.keyCode || theEvent.which; //瀏覽器兼容性
	if (code == 13) { //回車鍵的鍵碼值爲13
		$("#btnlogin").click(); 
		return false;	//這一句很重要!!!
	} 
};
$(function(){
$("#btnlogin").click(function (){
    if($("#loginForm").valid()){
	var url="${ctx}/checkLogin.action";
		$.post(
			url,
			{"user.username":$("#username").val(),"user.password":$("#password").val()},
			function(data){
				if(data.retcode=="0"){
					window.location.href="${ctx}/toUserList.action";
				}else{
					alert(data.retmsg);
					$("#username").select();
					//$("#username").focus();
				}
			}
		);		
    }		
});
});
</script>

以上是js代碼,可以實現回車登錄,或者單擊登錄雙重效果。

jsp的表單部分:

<s:form id="loginForm" action="checkLogin" method="post" namespace="/">
			<label for="username"><!-- for屬性實現單擊關聯字(工號)單聯輸入框獲取焦點 -->
				工  號:
			</label>
			<s:textfield id="username" name="user.username"/>
			<label for="password">
				密  碼:
			</label>
			<s:password id="password" name="user.password" /><br/>
			<input type="button" class="login-sub" value="" id="btnlogin" />
			<br/>
</s:form>

發佈了21 篇原創文章 · 獲贊 16 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章