2011-05-20 113 views
0

我有以下code.I顯示用戶一個fancybox,他們單擊提交表單我關閉fancybox,並顯示他們在頁面上處理,並使用jQuery做一個AJAX文章。但一些fancybox得到保持開放,直到AJAX表單發佈完成。這給用戶造成混淆。關閉FancyBox之前AJAX後

function BeginProcess(){ 
     $.fancybox.close(); 
     $("#imgProcess").attr("src", "/admin/images/loading_animation.gif"); 
     $('#imgProcess').show(); 
     PostAJAXForm(); 
} 

function PostForm(FormData){ 
       $.ajax({ 
       type: "POST", 
       url: "process_image.jsp", 
       data: FormData, 
       contentType: "application/x-www-form-urlencoded", 
       async: false, 
       success: function(response) { 
        response = $.trim(response); 
        $('#imgProcess').delay(10000).hide(); 
        return response;      
       }, 
       error: function(xhr, ajaxOptions, thrownError) { 
        alert("There was an error. Please undo image and perform action again. "); 
        $('#lblProcess').delay(5000).hide(); 
        return; 
        } 
       }); 
} 
+1

你可以包含PostAJAXForm()函數嗎?此外,如果您願意,可以將jQuery函數鏈接在一起。例如:'$(「#imgProcess」)。attr(「src」,「/admin/images/loading_animation.gif」)。show();' – raidfive 2011-05-20 15:33:31

+0

添加了郵政編碼。 – 2011-05-20 15:43:49

回答

0

你需要在jQuery的」阿賈克斯的beforeSend功能添加$.fancybox.close()

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/ajaxSend/

這應該幫助你太 http://snipplr.com/view/47979/close-a-fancybox-when-you-submit-a-form-with-ajax

+0

@Satish snipplr鏈接不正確。這隻會在從服務器收到響應後關閉fancybox。 – raidfive 2011-05-20 15:36:02

+1

同意,我認爲粘貼了一個錯誤的鏈接,下面是正確的一個,向下滾動以評論Andrew Wirick,他指向beforeSend:http://www.readmespot.com/question/o/3569439/close-fancybox-after-行動- – Satish 2011-05-20 15:40:35

+0

爲什麼它不會關閉,如果我做甚至在做Ajax請求之前呢? – 2011-05-20 15:43:34

1

這裏有一個片段:

var params = $(this).serialize(); 
var self = this; 

$.ajax({ 
      type: 'POST', 
      url: self.action, 
      data: params, 
      beforeSend: function(){ 
        alert('Closing'); 
        $.fancybox.close(); 
      }, 
      success: function(data){ 
        alert('Finished processing form'); 
        return false; 
      }, 
      dataType: 'json' 
    }); 

很抱歉,如果事情搞砸從該片段中刪除或刪除 - 但你應該明白。