2013-03-20 78 views
1

我有一個場景,我在ajax回調事件中將html加載到div中。我的目標是在主內容加載完成後,將另一個div附加到新加載的div上。主要的問題是有很多html被加載,我不得不等待直到加載發生。用ajax調用內容加載div後觸發事件

基本步驟如下。

@using (Ajax.BeginForm 
(
    Model.PostAction, 
    Model.PostController, 
    null, 
    new AjaxOptions() { OnSuccess = "contentLoadSuccess", OnFailure = "contentLoadFailure" }, 
    new { id = "reportBase_frmViewer", name = "reportBase_frmViewer" }) 
) 
{ 
    ... 
} 

function contentLoadSuccess(ajaxContext) { 
    showWaitIndicator(false); 
    if (ajaxContext.Format == null) 
     //If the format is not there then the server reponded but the an unhandled exception was caught and thrown 
     //the content will be the user friendly version 
     setReportContent(ajaxContext); 
    else { 
     if (ajaxContext.Format == "HTML") { 
      //If the format is HTML then it is already in the result, inject it into the content container 
      showWaitIndicator(true); 
      updatePageNumber(ajaxContext.PageNumber, ajaxContext.TotalPageNumber); 
      setReportContent(ajaxContext.HTMLContent); 
     } else 
      //Export option is used. The render link has a url friendly format to start the file download 
      window.location.href = ajaxContext.RenderLink + "&format=" + ajaxContext.Format; 
    } 
} 

function setReportContent(content) { 
     $("#reportContent").html(content); 
     $("#toggleToolbar").appendTo("#reportContent");//<- Does not work content takes to long and will overwrite appended div 
     $("#reportContent").show();   
    } 

$(document).ready(function() { 
    //This does not work either 
    $("#reportContent").load(function (e) { 
     $("#toggleToolbar").appendTo("#reportContent"); 
     $("#toggleToolbar").show();     
    }); 
}); 

$("#reportContent").on("load", function (event) { 
    alert("This does not work either"); 
    $("#toggleToolbar").appendTo("#reportContent"); 
}); 

我看過帶加載觸發器的.live(),但是我讀到它正在折舊。也許有一種我沒有想過的完全合乎邏輯的方式。或者也許我正在以這種錯誤的方式去做。任何幫助,將不勝感激。

回答

0

嘗試使用超時

function setReportContent(content) { 
    $("#reportContent").html(content); 
    setTimeout(function(){ 
     $("#toggleToolbar").appendTo("#reportContent");//<- Does not work content takes to long and will overwrite appended div 
     $("#reportContent").show();   
    }, 200); 
}