2013-03-20 55 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); 
}