![](/img/trans.png)
[英]Why doesn't my view load the jquery code from _Layout.cshtml but does when I add it lcoally to the view?
[英]jQuery script not recognised when injected in my view (.cshtml)
通常,我為腳本創建一個javascript文件(myfile.js)。
例:
/// <reference path="../../Scripts/Jquery/jquery-1.5.1.js" />
$(document).ready(function () {
// bind 'myForm' and provide a simple callback function
$('#uploadBackgroundForm').ajaxForm({
iframe: true,
dataType: "json",
success: BackgroundUploadedSuccess,
error: BackgroundUploadedError
});
});
現在,我想將腳本直接放在部分(<script type =“ text / javascript”> ..... </ script>)的視圖(.cshtml)中。
例:
...my view goes here...
<script type="text/javascript">
/// <reference path="../../Scripts/jquery-1.5.1.js" />
$(document).ready(function () {
// bind 'myForm' and provide a simple callback function
$('#uploadBackgroundForm').ajaxForm({
iframe: true,
dataType: "json",
success: BackgroundUploadedSuccess,
error: BackgroundUploadedError
});
});
....
</script>
但這是行不通的。 我的意思是,當我將光標放在“ ready”之類的jQuery語法上並單擊CTRL + J時,無法識別任何內容。
任何想法?
謝謝。
您必須使用以下構造添加引用:
@if (false)
{
<script src="~/Scripts/jquery-1.5.1.js" type="text/javascript"></script>
}
使用@if (false)
不會在運行時加載腳本...(您也可以看到此頁面 )。
更新:您必須在其他腳本標簽之前插入此代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.