jquery。不止一次提交?

我有以下代码:

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

然而,它工作得很好,当用户再次推送“保存”时,好像是$('.save').submit(); 是不是再次被解雇….我至少没有在messageBox看到挂起或成功消息。

我错过了什么?

编辑

我想到了。 当我fadeOut ,我并没有淡出错误div,我正在淡出整个messageBox div,所以一旦它display:none ,就没有什么messageBox它带回来。

最有可能的问题是你的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/