jquery。不止一次提交?

时间:2011-05-26 18:29:13

标签: jquery forms post submit

我有以下代码:

$('.save').submit(function(e){
    var data = 'id=' + $(this).attr('name') + '&' + $(this).serialize();
    var messageBox = $(this).next('.message');
    messageBox.html('<div class="pending">Saving... please wait...</div>');
    $.post('save.php', data,
    function(response) {
        messageBox.html(response).delay(3000).fadeOut('slow');
    });
    e.preventDefault();
});

然而,它工作得很好,当用户再次推送“保存”时,似乎$('.save').submit();没有再被解雇....我至少没有看到一个待处理或成功的消息messageBox

我错过了什么?

修改

我明白了。当我fadeOut时,我没有淡出错误div,我正在淡出整个messageBox div,所以一旦它display:none,就没有任何东西把它带回来。

1 个答案:

答案 0 :(得分:1)

最有可能的问题是你的AJAX没有成功回归。添加.error()函数。

来自jquery docs的示例代码:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post("example.php", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });

http://api.jquery.com/jQuery.post/

相关问题