<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>