在单个JS文件中有两个AJAX表单提交,只有一个正在工作

时间:2016-02-10 09:37:59

标签: javascript jquery ajax forms

在HTML中,表单看起来完全相同,只是表单ID已更改。我使用这个JS文件使用AJAX提交表单:

$("#change-password").submit(function(e) {
    var url = "http://domain/actions"; 
    $.ajax({
        type: "POST",
        url: url,
        data: $("#change-password").serialize(), 
        success: function(data) {
            $("div.change-password-response").html(data);
        }
    });
    e.preventDefault(); 
});

$("#change-email").submit(function(e) {
    var url = "http://domain/actions"; 
    $.ajax({
        type: "POST",
        url: url,
        data: $("#change-email").serialize(), 
        success: function(data) {
            $("div.change-email-response").html(data);
        }
    });
    e.preventDefault(); 
});

HTML部分

                <div class="col-md-6 col-sm-6 add_bottom_30">
                    <h4>Schimbare parola</h4>
                    <form id="change-password" method="post">
                    <div class="form-group">
                        <label>Parola veche</label>
                        <input class="form-control" name="old_password" id="old_password" type="password" autocomplete="off">
                    </div>
                    <div class="form-group">
                        <label>Parola noua</label>
                        <input class="form-control" name="new_password" id="new_password" type="password" autocomplete="off">
                    </div>
                    <div class="form-group">
                        <label>Confirma parola noua</label>
                        <input class="form-control" name="confirm_new_password" id="confirm_new_password" type="password" autocomplete="off">
                    </div>
                    <input type="hidden" name="action" value="change-password">
                    <button type="submit" class="btn_1 green">Actualizeaza parola</button>
                    <div class="change-password-response form-response"></div>
                    </form>
                </div>

                <div class="col-md-6 col-sm-6 add_bottom_30">
                    <h4>Schimbare adresa email</h4>
                    <form id="change-email" method="post">
                    <div class="form-group">
                        <label>Email vechi</label>
                        <input class="form-control" name="old_email" id="old_email" type="text" autocomplete="off">
                    </div>
                    <div class="form-group">
                        <label>Email nou</label>
                        <input class="form-control" name="new_email" id="new_email" type="text" autocomplete="off">
                    </div>
                    <div class="form-group">
                        <label>Confirma email nou</label>
                        <input class="form-control" name="confirm_new_email" id="confirm_new_email" type="password" autocomplete="off">
                    </div>
                    <input type="hidden" name="action" value="change-email">
                    <button type="submit" class="btn_1 green">Actualizeaza Email</button>
                    <div class="change-email-response form-response"></div>
                    </form>
                </div>

现在,我的问题是表单1正在运行,表单2没有触发AJAX并且正常工作。我做错了什么?

1 个答案:

答案 0 :(得分:1)

检查#change-email是否有其他绑定事件。

Firebug 中,检查#change-email元素并转到Event标签。
Chrome 中,检查该元素并转到Event Listeners标签。

您将看到所有绑定事件和相应的回调函数。

如果无效,请尝试从Console

手动提交表单
  1. 检查表单,然后运行
  2. $($0).submit();
  3. 如果它可以工作,看起来你的按钮上有一些事件。

相关问题