2011-08-11 26 views
1

我有一个jQuery ajax脚本来处理提交表单。但是,我有一个回调,以在ajax请求期间添加“处理...”和“完成”等。但是,完成后脚本应该将edit button添加回人员可以再次使用该窗体,但是它将添加按钮两次。这个回调为什么发射两次?

这是Ajax请求开始的地方,该意见将帮助您按照

$.ajax({ 

    url: "../ajax/update_site_info.php?id=" + $('[name="site_id"]').val() + "&field=" + $(parent).children("input").attr("name") + "&value=" + $(parent).children("input").val(), 
    cache: false, 
    success: function(response){ 

     //alert('success'); 

     // On success, fade out the "Saving..." 
     $(parent).children('.message').fadeOut(150, function(){ 

      //alert('message'); 

      // Remove the save button (submit button) 
      $(parent).children(".jUpdateSave").remove(); 

      // Add "Saved!" 
      $(parent).append('<span class="message">' + response + '</span>'); 

      // Let "Saved!" linger for a while before it fades out 
      $(parent).children('.message').delay(1500).fadeOut(250, function(){ 

       //alert("stop"); // THIS IS WHERE THINGS HAPPEN TWICE 

       // Put the edit button back 
       $(parent).append('<a class="jUpdate"><img src="images/shared/icon_edit.png" width="16" height="12" alt=""></a>'); 

       // Remove the "Saved1" message 
       $(parent).children('.message').remove(); 
      }); 
     }); 
    } 
}).error(function(){ 

    $(".message").text("There was an error proccessing your request. [0]"); 
}); 

完整的脚本是here

除了最后一次回调发生两次(它会提示stop两次)之外的所有工作。

有什么想法?

+0

在class'message'的'parent'下有多个孩子吗?没有HTML代码很难说。 – AppleGrew

回答

4

是否

$(parent).children('.message') 

返回两个元素?

+0

啊哈!我在做'$(parent).append'(''+ response +'');'在删除旧消息之前,在页面上引发两个'消息'。谢谢! –

3

您打电话给.append()每个.message。我假设你有两个,因为你在这里引用一个:

$(parent).children('.message').fadeOut(150, function(){ 

这里追加一个:

$(parent).append('<span class="message">' + response + '</span>'); 

该按钮将用于与.message类的每个元素进行重新创建。

要解决该问题,请避免使用.message类创建多个子元素,或者将您的delay()/fadeOut()语句仅运行一次。

0

在发出多个请求的ajax响应之前,我认为页面上有超过1个.message的跨度。只是为了解决这个问题。

// On success, fade out the "Saving..." 
     $(parent).children('.message').fadeOut(150, function(){ 

      //alert('message'); 

      // Remove the save button (submit button) 
      $(parent).children(".jUpdateSave").remove(); 

      // Add "Saved!" 
      $(parent).append('<span class="message">' + response + '</span>'); 

      // Let "Saved!" linger for a while before it fades out 
      $(parent).children('.message:first').delay(1500).fadeOut(250, function(){ 

       //alert("stop"); // THIS IS WHERE THINGS HAPPEN TWICE 

       // Put the edit button back 
       $(parent).append('<a class="jUpdate"><img src="images/shared/icon_edit.png" width="16" height="12" alt=""></a>'); 

       // Remove the "Saved1" message 
       $(parent).children('.message:first').remove(); 
      }); 
     }); 
相关问题