0

.NetMVC3でスクリプトを実行しようとしています。スクリプトは部分的に表示されています。Html.RenderActionを使用してpartialを呼び出すと機能しますが、jqueryajaxによってページの部分ビューを呼び出すと機能しません。

部分図

   <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>

私は2番目の方法を使用する必要があります..問題は何ですか?

4

1 に答える 1

0

これは、<div id="content">が dom にロードされていないためです。content div の後に JS スクリプトを配置してみてください

<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>
于 2013-01-09T09:02:34.027 に答える