2012-06-13 47 views
3

我試圖用一個按鈕點擊通過javascript調用時顯示一個GIF動畫div。但問題是,它只能在Firefox中正常工作。在IE和Chrome中,動畫不會顯示出來。我試着用alert(「stop」)來調試;動畫之前和之後,它確實工作,但我刪除警報後,它不會工作了。有什麼建議嗎?動畫不工作在IE瀏覽器時,jQuery函數內調用js函數

下面是該TestPage.aspx的片段:

<div id="animationDiv" style="display: none;"> 
    <img src="loading.gif" /> 
</div> 
... 
<div> 
    <asp:Button ID="testButton" runat="server" Text="Test AJAX" OnClientClick="return testButtonClick();" /> 
</div> 
... 
<script type="text/javascript"> 
<!-- 
    var docWidth, docHeight; 

    function testButtonClick() { 
     var o_animationDiv = $("#animationDiv"); 

     o_animationDiv.width(docWidth); 
     o_animationDiv.height(docHeight); 
     o_animationDiv.show(); 

     $.ajax({ 
      type: "Post", 
      url: "TestPage.aspx/TestAjax", 
      async: false, 
      cache: false, 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: testAjaxSuccess, 
      error: testAjaxError 
     }); 

     function testAjaxSuccess(data, status, xhr) { 
      // do something here 
     }; 

     function testAjaxError(xhr, status, error) { 
      // do something here 
     }; 

     o_animationDiv.hide(); 

     return false; 
    }; 

    $(document).ready(function() { 
     docWidth = $(document).width(); 
     docHeight = $(document).height(); 
    }); 
//--> 
</script> 

下面是TestPage.aspx.cs的片段:

// using PageMethod for ajax 
[System.Web.Services.WebMethod(EnableSession = true)] 
public static string TestAjax() 
{ 
    System.Threading.Thread.Sleep(5000); // 5 seconds 

    // or do something else here 
    // ie. if validation error, throw an exception 

    return string.Empty; 
} 

更新1:增加了一些JavaScript函數

+0

我面臨同樣的問題,如果你得到解決方案,然後幫我解決它。 –

回答

0

jQuery的AJAX是異步,這樣就不會等待ajax結果再次隱藏。

o_animationDiv.hide(); 
o_animationDiv.show(); 

此行以串行方式工作,您不能看到它是否正常工作。所以,你應該等待AJAX​​結果隱藏it.Try此,

$.ajax({ 
      type: "Post", 
      url: "TestPage.aspx/TestAjax", 
      async: false, 
      cache: false, 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json" 
     }).done(function() { 
      o_animationDiv.hide(); 
     }); 

UPDATE:

對不起,我錯過了你創建一個同步Ajax請求的地步。 但是有一個問題。運行同步XHR時,大多數瀏覽器會自行鎖定。這也會影響以前的進程。

也許如果你使用beforeSend,就可以避免這個問題。

beforeSend

A pre-request callback function that can be used to modify the jqXHR 
(in jQuery 1.4.x, XMLHTTPRequest) object before it is sent 

試試這個,

$.ajax({ 
      type: "Post", 
      url: "TestPage.aspx/TestAjax", 
      async: false, 
      cache: false, 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      beforeSend : function() { 
       o_animationDiv.show(); 
      } 
     }).done(function() { 
      o_animationDiv.hide(); 
     }); 

更新2:

如果不行,試試這個,

... 
o_animationDiv.width(docWidth); 
o_animationDiv.height(docHeight); 
o_animationDiv.show(); 

setTimeout(function() { 
     $.ajax({ 
      type: "Post", 
      url: "TestPage.aspx/TestAjax", 
      async: false, 
      cache: false, 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: testAjaxSuccess, 
      error: testAjaxError 
     }).done(function() { 
      o_animationDiv.hide(); 
     }); 
}, 600); 
+0

CMIIW。這是同步調用,因爲我將它設置爲$ .ajax函數async:false。所以,在執行下一行之前,應該先等待ajax。 – Roby

+0

@Roby,是的,我沒有看到你設置了錯誤的異步。再看看我更新的答案。 – ocanal

+0

:-(它仍然沒有工作,我已經添加了「beforeSend」和「done」部分,但div仍然沒有顯示出來[ – Roby

0

你爲什麼不叫.hide()的崗位上取得成功:

$.ajax({ 
    type : "Post", 
    url : "TestPage.aspx/TestAjax", 
    cache : false, 
    data : "{}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(data) { 

    // hide element when POST return as arrived, 
    // meaning the "5 seconds" period has ended! 
    o_animationDiv.hide(); 

    } 
}); 

你可以在jQuery documentation瞭解回調函數!

+0

我試過它與類似的功能,這些行,它仍然無法正常工作。我試過的一個:成功:TestAjaxSuccess,錯誤:TestAjaxError。 TestAjaxSuccess和TestAjaxError都是一個函數。 – Roby

+0

你的文章將會做什麼精確的?只需等待x秒,不返回任何內容?或者一些實際的交互會發生在那裏? – Zuul

+0

實際上,在按鈕點擊功能中會有一到三個ajax調用,具體取決於哪個按鈕被點擊。例如,如果點擊一個保存按鈕,那麼它會按順序執行此操作:調用save pagemethod(在這裏輸入被驗證,如果成功,將被保存),如果成功,則更改按鈕狀態(即禁用按鈕)並加載保存的數據,如果出現錯誤,則顯示從服務器返回的錯誤消息。 – Roby

相關問題