第一次单击jquery ajax表单提交请求不起作用

时间:2011-01-20 09:40:03

标签: jquery ajax

向所有人致意,

我正在使用monorail + jquery开发一个应用程序,我发现ajax在jquery上运行时出现问题。

我有一个表单将它自己提交到div容器中,通常工作正常,但是每隔一段时间表单就不会在第一次点击时提交,我需要再次点击才能获得它工作

我已经使用Firebug进行了检查,请求仍在处理,如下图所示: http://www.freeimagehosting.net/uploads/c6f500d617.jpg

如果我再次按下提交按钮,则处理请求。

我正在使用的代码就是这样:

    $('.formSteps').submit(function () { // catch the form's submit event
    $.ajax({ // create an AJAX call...
        data: $(this).serialize(), // get the form data
        type: $(this).attr('method'), // GET or POST
        url: $(this).attr('action'), // the file to call
        success: function (response) { // on success..
            $('#service_plan_wizard_container').html(response); // update the DIV
        }
    });
    return false; // cancel original event to prevent form submitting
});

正如您可能猜到的那样,有一个名为service_plan_wizard_container的div,每个表单都会使用formSteps类。

此致

2 个答案:

答案 0 :(得分:0)

尝试将此添加到您的代码中:

context: this,

正上方

data: $(this).serialize(), // get the form data

var that = this;

在提交触发器下方,然后使用“that”而不是(this)

答案 1 :(得分:0)

我刚才遇到了同样的问题。我希望这篇文章可以帮助我,但我找不到任何东西(这很少)会有所帮助。

我已经弄明白了为什么。

在ajax请求中的某处插入:

async: false,

这完全解决了这件事。

顺便说一下,我意识到如果你把它放到你的ajax请求中,你实际上可以在第一次点击时得到一条错误信息:

error: function(er, err, error){
      alert(error);
},

我没有得到服务器响应的原因不是因为它没有触发,而是因为它返回错误而不是“成功”响应。

此外,在调试时,我非常推荐使用FireBug for Firefox,您可以在(Net-> XHR)选项卡中监控任何Ajax请求。非常有帮助!