.net
  • asp.net-mvc
  • jquery
  • 2013-01-09 81 views 0 likes 
    0

    在.Net MVC 3我嘗試運行一個腳本..該腳本是在局部視圖。如果我調用Html.RenderAction部分它的作品,但如果我通過jQuery的ajax調用頁面中的部分視圖它不工作。腳本不加載,如果部分視圖通過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> 
    

    我需要使用第二種方法..問題是什麼?

    回答

    0

    這是因爲<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> 
    

    後,也可以wrapp您的Ajax請求文檔準備事件處理程序

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

    我試過頁的通話AJAX腳本結束,但它仍然沒有工作。 –

    +0

    仍然沒有工作:( –

    +0

    如果我只寫在腳本警報(「你好世界」)它的工作..我認爲腳本不加載? –

    相關問題