简体   繁体   English

jQuery - 在ajax调用之后恢复表单提交

[英]jQuery - Resume form submit after ajax call

Is it possible to stop a form from submitting and then resubmitting the same form from within the success of an ajax call? 是否可以阻止表单提交,然后在ajax调用成功中重新提交相同的表单?

At the moment it gets to the success bit but it doesn't resubmit the form which should submit and redirect the user to the http://example.com website. 目前它获得了成功,但它没有重新提交应该提交的表单并将用户重定向到http://example.com网站。

Thank you very much for any help in advance 非常感谢您提前提供任何帮助

If it's not possible to do it this way, is there another way of getting it to work? 如果不可能这样做,还有另一种方法可以让它发挥作用吗?

$(document).ready(function() {
    $('form').submit(function(e) {
        e.preventDefault();

        $.ajax({
            url: $('form').attr('action'),
            type: 'post',
            data: $('form').serialize(),
            success: function(data) {
                if (data == 'true')
                {
                    $('form').attr('action', 'http://example.com');
                    $('form').unbind('submit').submit(); // mistake: changed $(this) to $('form') - Problem still persists though it does not resubmit and redirect to http://example.com
                }
                else
                {
                    alert('Your username/password are incorrect');
                }
            },
            error: function() {
                alert('There has been an error, please alert us immediately');
            }
        });
    });
});

Edit: 编辑:

Stackoverflow posts checked out for the code below: Stackoverflow帖子签出以下代码:

I just thought I'd mention I have also tried this code without avail. 我只是想我提到我也试过这个代码而没有用。

var ajaxSent = false;
$(document).ready(function() {
    $('form').submit(function(e) {

        if ( !ajaxSent)
            e.preventDefault();

        $.ajax({
            url: $('form').attr('action'),
            type: 'post',
            data: $('form').serialize(),
            success: function(data) {
                if (data == 'true')
                {
                    alert('submit form');
                    ajaxSent = true;
                    $('form').attr('action', 'http://example.com');
                    $('form').submit();
                    return true;
                }
                else
                {
                    alert('Your username/password are incorrect');
                    return false;
                }
            },
            error: function() {
                alert('There has been an error, please alert us immediately');
                return false;
            }
        });
    });
});

I have also tried this code without any luck as well. 我也试过这个代码而没有任何运气。

$(document).ready(function() {
    $('form').submit(function(e) {
        e.preventDefault();

        $.ajax({
            url: $('form').attr('action'),
            type: 'post',
            data: $('form').serialize(),
            success: function(data) {
                if (data == 'true')
                {
                    $('form').attr('action', 'http://example.com');
                    $('form').unbind('submit').submit();
                    return true;
                }
                else
                {
                    alert('Your username/password are incorrect');
                    return false;
                }
            },
            error: function() {
                alert('There has been an error, please alert us immediately');
                return false;
            }
        });
    });
});

Solution was quite simple and involved adding and setting async to false in .ajax(). 解决方案非常简单,涉及在.ajax()中添加和设置async为false。 In addition, I have re-worked the code to work of the submit button instead which submits the form when the AJAX passes successfully. 另外,我已经重新编写了代码来处理提交按钮,而是在AJAX成功通过时提交表单。

Here is my working code: 这是我的工作代码:

$(document).ready(function() {
    var testing = false;
    $('#btn-login').on('click', function() {
        $.ajax({
            url: $('form').attr('action'),
            type: 'post',
            data: $('form').serialize(),
            async: false,
            success: function(data) {
                if (data == 'true')
                {
                    testing = true;
                    $('form').attr('action', 'https://example.com');
                    $('form').submit();
                }
                else
                {
                    alert('Your username/password are incorrect');
                }
            },
            error: function() {
                alert('There has been an error, please alert us immediately');
            }
        });

        return testing;
    });
});

It's no good practice to reselect all form tags throughout your code, what if you have multiple forms on the page? 在整个代码中重新选择所有表单标签是不错的做法,如果页面上有多个表单,该怎么办? Also you'd better use .on() and .off() with jQuery. 另外你最好用.on().off()使用jQuery。

$(document).ready(function() {
    $('form').on('submit', function(e) {
        e.preventDefault();

        // cache the current form so you make sure to only have data from this one
        var form = this,
            $form = $(form);

        $.ajax({
            url: form.action,
            type: form.method,
            data: $form.serialize(),
            success: function(data) {
                if (data == 'true')
                {
                    $form.attr('action', 'http://example.com').off('submit').submit();
                }
                else
                {
                    alert('Your username/password are incorrect');
                }
            },
            error: function() {
                alert('There has been an error, please alert us immediately');
            }
        });
    });
});

In one line you use $('form') to select the form to change its action, but then you use $(this) to try to select that same form. 在一行中,您使用$('form')来选择表单来更改其操作,但之后您使用$(this)来尝试选择相同的表单。 I would guess that this inside the callback function isn't what you expect it to be, and is something other than your form (possibly the window object). 我猜想回调函数里面的this不是你想象的那样,而是你的表单以外的东西(可能是window对象)。

Just chain the calls: 只是链接电话:

$('form').attr('action', 'http://example.com').unbind('submit').submit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM