我有一个使用 Twitter Bootstrap 在模式窗口中打开的登录表单。Enter 键使窗口关闭而不是提交表单。我尝试使用 javascript 来捕获按键并提交表单,但事件侦听器似乎永远不会触发。我究竟做错了什么?
<!-- Modal -->
<div id="loginModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="loginModalLabel">TTU</h3>
</div>
<form class="loginLoginForm form-inline" action="html-email/tt-facstaff" method="post">
<fieldset class="loginLoginFieldset">
<div class="modal-body">
<div class="loginForm">
<div class="loginMessage"></div>
<div class="loginLogin">
<legend class="loginLegend">Login</legend>
<label class="loginUsernameLabel">
<div class="input-append">
<input class="loginUsername input-medium" type="text" name="username" placeholder="username..." />
<span class="add-on"><del>@tntech.edu</del></span>
</div>
</label>
<label class="loginPasswordLabel">
<input class="loginPassword input-medium" type="password" name="password" placeholder="password..." />
</label>
<input class="returnUrl" type="hidden" name="returnUrl" value="/html-email/tt-facstaff" />
<input class="loginLoginValue" type="hidden" name="service" value="login" />
<label class="checkbox">
<input type="checkbox" name="rememberMe"> Remember Me
</label>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<span class="loginLoginButton"><input id="loginBtn" class="btn btn-primary" type="submit" name="Login" value="Login" /></span>
</div>
</fieldset>
</form>
<style type="text/css">
#loginResponse{display:none;}
</style>
<script type="text/javascript">
$("#loginForm *").keypress(function(e){
e.preventDefault();
console.log(e);
if((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)){
$("#loginBtn").click();
return false;
} else {
return true;
}
});
</script>
</div>