提交表单数据上都点击进入并击中从引导模式窗口提交按钮

问题描述:

下面是我的形式,在我的模式窗口:

Here's my form that's in my modal window:

我没有关闭按钮,我已经从收盘于pressing ESC关闭该窗口。我希望能够提交关于pressing提交表单数据或pressing回车键。

I have no close button and I have disabled the window from closing on pressing esc. I want to be able to submit the form data on pressing submit or on pressing the enter key.

<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-header">
        <h3 id="myModalLabel">Enter your Credentials</h3>
    </div>
    <div class="modal-body">
        <form id="login-form" class="form-horizontal" accept-charset="UTF-8"  data-remote="true"">
            <div class="control-group">
                <label class="control-label" for="inputEmail">Email</label>
                <div class="controls">
                    <input type="email" id="email" name="email" value=""  placeholder="Email">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputPassword">Password</label>
                <div class="controls">
                    <input type="password" id="passwd" name="passwd" value="" placeholder="Password">
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <label class="checkbox">
                        <input type="checkbox"> Remember me
                    </label>
                    <input type="submit"  id="login" value="Login"class="btn btn-primary" />

                </div>
            </div>
        </form>          
    </div>
</div>

下面是我使用的脚本:

$(document).ready(function(){
    $('#myModal').modal('show');
});

function submitLogin() {
    $.ajax({
        url:"login_mysql.php",
        type:'POST',
        dataType:"json",
        data: $("#login-form").serialize()
    }).done(function(data){
        //do something
    });
}

$('#passwd').keypress(function(e) {
    if (e.which == '13') {
        submitLogin();
    }
});

$('#login').click(function(){
    submitLogin();
});

在pressing键入我的密码或单击提交按钮相同的页面重新加载与AJAX脚本不运行后进入。可有人告诉我,如果我的脚本冲突与我的模态窗口的默认设置?

On pressing enter after keying in my password or clicking on the submit button the same page reloads and the ajax script does not run. Could someone tell me if my script is *ing with the default settings of my modal window?

听成提交事件 - 它被双方进入后点击事件触发

Listen to form submit event - it gets triggered by both enter and click events.

标记

<form id="yourForm">
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="submit" value="Submit">
</form>

JS

$('#yourForm').submit(function(event){

  // prevent default browser behaviour
  event.preventDefault();

  //do stuff with your form here
  ...

});