簡體   English   中英

如果通過jQuery Ajax調用部分視圖,則腳本不會加載

[英]script not load if partial view call by jquery ajax

在.Net MVC 3中,我嘗試運行腳本。該腳本處於部分視圖中。 如果我用Html.RenderAction調用部分視圖,它可以工作,但是如果我通過jquery ajax在頁面中調用部分視圖,則它不起作用。

部分視圖

   <script type="text/javascript">
    document.write("<script src='http://ad.reklamport.com/rpgetad.ashx?tt=t_iddaa_habersayfalari_mac_detay_300x250&ciid=&rnd=" + Math.random() % 99999999 + "'></" + "script>");            
</script> 

視圖

有用..

<html>
    <head>        

    </head>
    <body>        
        @{Html.RenderAction("Partial");}

    </body>


</html>

它不起作用...

<body>        
        @{Html.RenderAction("Partial");}
        <script type="text/javascript">
            $.ajax({
                type: "post",
                url: "/Home/Partial",
                dataType: "html",                
                success: function (result) {                    
                    $("#content").html(result);
                },
                error: function (result) {
                }
            });
        </script>
        <div id="content">

        </div>
    </body>

我需要使用第二種方法。.什么問題?

這是因為<div id="content">尚未加載到dom。 嘗試將JS腳本放在內容div之后

<body>        
        @{Html.RenderAction("Partial");}

        <div id="content">

        </div>
 <script type="text/javascript">
            $.ajax({
                type: "post",
                url: "/Home/Partial",
                dataType: "html",                
                success: function (result) {                    
                    $("#content").html(result);
                },
                error: function (result) {
                }
            });
        </script>
    </body>

或者您可以包裝您的ajax請求文檔就緒事件處理程序

<script type="text/javascript">
           $(function{
                $.ajax({
                    type: "post",
                    url: "/Home/Partial",
                    dataType: "html",                
                    success: function (result) {                    
                        $("#content").html(result);
                    },
                    error: function (result) {
                    }
                });
            });
 </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM