[英]$(document).ready works when inserted into _Layout.cshtml :: doesn't work when inserted into Index.cshtml
我知道如果它是一條蛇,它會讓我咬傷。 但我不明白為什么它在_Layout頭文件中有效,但在我需要的特定索引頁面中卻沒有。 我無法在使用_Layout.cshtml的每個頁面中看到此代碼。
這是在_Layout中使用IF的jQuery代碼。 我將其剪切並粘貼到Index.cshtml中,它甚至不會發出警報。 預期要做的事情:如果其他輸入發生變化(只能通過名稱或ID搜索),它會清除輸入。
這里沒有代表的是圍繞javascript的標簽。 我嘗試了一個關閉所有其他代碼的警報。 由於某種原因,javascript標記沒有觸發。
$(document).ready(function () {
alert('doc ready in _layout just fired');
$('#SearchString').on('input', function () {
$('#SearchID').val('');
});
$('#SearchID').on('input', function () {
$('#SearchString').val('');
});
});
這在Index.cshtml中不起作用:
<script type="text/javacript">
alert('code in Index test');
</script>
======================================
所以,這就是:我將單頁所需的jQuery代碼放在“.js”文件中,除了代碼之外什么也沒有。 然后我在我的Index.cshtml頂部引用了這個文件,它可以工作:
<script src="@Url.Content("~/Scripts/search.js")" type="text/javascript"></script>
現在,這是一個很好的解決方法,為那些喜歡它的人提供了一定程度的分離,但我討厭說這是為什么Index.cshtml繞過javacript標簽並使用jQuery代碼的答案。 所以我沒有回答我的問題,只是這樣說它可能會幫助其他幾乎全部拔掉頭發的人。 。 。 。
你在使用剃須刀引擎嗎? 這就是我通常擁有的......
在_layout.chstml中
<head>
@RenderSection("Head", false)
</head>
然后在你看來
@section Head {
<script type="text/javascript">
$(document).ready(function () {
// your code goes here
});
</script>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.