1

我有一个场景,我在 ajax 回调事件中将 html 加载到 div 中。我的目标是在加载主要内容后将另一个 div 附加到新加载的 div 上。主要问题是加载了很多 html,我必须等到加载发生。

基本步骤如下。

@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() ,但我读到它正在折旧。也许有一种我没有想到的完全合乎逻辑的方式。或者也许我正在以错误的方式解决这个问题。任何帮助,将不胜感激。

4

1 回答 1

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);
}
于 2013-03-20T03:51:21.977 回答