使用回车键提交表单

时间:2013-02-07 22:15:08

标签: javascript jquery html forms twitter-bootstrap

我有一个使用Twitter Bootstrap在模态窗口中打开的登录表单。输入键使窗口关闭而不是提交表单。我已经尝试使用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>

0 个答案:

没有答案