2012-06-13 29 views
0

我正在使用ASP MVC3爲其使用部分視圖,但問題是我無法加載到相同的腳本。 下面是其中我已經嘗試的代碼:部分視圖MVC3的腳本無法加載

<script type="text/javascript"> 
    $('#bodytag').load(function() { 
     alert('Load was performed.'); 
    }); 
</script> 
<div class="acc-expand-bkg" id="bodytag"> 
    <div class="submenu_dropdown clearfix"> 
     <div class="filter_bar clearfix"> 
      <div class="left" style="margin-left: 10px;"> 
       <label style="width: auto; margin-top: 7px;"> 
        Standard:</label> 
       @Html.DropDownList("teamDetailId", ViewBag.TeamNames as SelectList, null, new { id = "teamDetailId" }) 
      </div> 
     </div> 
    </div> 
</div> 

上述局部視圖是沒有得到裝載腳本。我嘗試過使用document.ready,但它沒有爲它工作。 你能告訴我同樣的工作嗎?

預先感謝您

+0

如果您將腳本塊放在父div下面,它會工作嗎? – joshschreuder

回答

1

嘗試這樣的:

<script type="text/javascript"> 
    alert('Load was performed.'); 
</script> 

但通常的腳本不應該放在局部視圖內。他們不應該與標記混合。通常腳本屬於單獨的JavaScript文件。因此,一個更好的辦法是在一個單獨的文件中定義JavaScript函數:

function doSomething() { 
    alert('Load was performed.'); 
} 

,然後調用這個javascript函數,一旦你使用加載的局部視圖AJAX:

$.ajax({ 
    url: 'some url', 
    type: 'post', 
    success: function(result) { 
     $('#someId').html(result); 
     // the partial view was injected into the DOM => now we could 
     // invoke our function: 
     doSomething(); 
    } 
}); 
0

load功能用於使用ajax從服務器加載內容並更新指定html元素的內部內容。您必須將服務器url作爲該方法的第一個參數傳遞。

$('#bodytag').load('ajax/test.html', function() { 
    alert('Load was performed.'); 
}); 

您必須將腳本塊放在html下面。