插入到_Layout.cshtml中的$(document).ready工作原理在插入到Index.cshtml中时不起作用 [英] $(document).ready works when inserted into _Layout.cshtml :: doesn't work when inserted into Index.cshtml

查看:94
本文介绍了插入到_Layout.cshtml中的$(document).ready工作原理在插入到Index.cshtml中时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果它是一条蛇,它会让我咬伤。但我不明白为什么它在_Layout头文件中有效,但在我需要的特定索引页面中却没有。我看不到在使用_Layout.cshtml的每个页面中都有这个代码。

I know if it was a snake it would have bit me. But I can't see why it works in _Layout header file, but not in specific Index page where I need it. I can't see having this code in every page that uses the _Layout.cshtml.

这是在_Layout中使用IF的jQuery代码。我将其剪切并粘贴到Index.cshtml中,它甚至不会发出警报。它应该做什么:如果其他输入发生变化(只能通过名称或ID搜索),它会清除输入。

Here is jQuery code that works IF in _Layout. I cut and paste it into Index.cshtml and it won't even fire an alert. What it is expected to do: It clears an input if the other input changes (can only search by either name or ID).

此处未表示的是javascript周围标​​记。
我尝试了一个关闭所有其他代码的警报。
由于某种原因,javascript标记没有触发。

not represented here are the javascript surrounding tags. I tried just an alert with all other code turned off. For some reason the javascript tag is not firing.

$(document).ready(function () {

    alert('doc ready in _layout just fired');

            $('#SearchString').on('input', function () {
                $('#SearchID').val('');
            });

            $('#SearchID').on('input', function () {
                $('#SearchString').val('');
            });
});

这在Index.cshtml中不起作用:

This doesn't work in Index.cshtml wherever it is placed:

<script type="text/javacript">
alert('code in Index test');
</script>

===================== =================

======================================

所以,这只是在:
我把单个所需的jQuery代码.js文件中的页面,但代码中没有任何内容。
然后我在我的Index.cshtml顶部引用了这个文件并且它可以工作:

So, this just in: I put the jQuery code needed for single page in a ".js" file with nothing in there but the code. Then I referenced this file at the top of my Index.cshtml and it works:

  <script src="@Url.Content("~/Scripts/search.js")" type="text/javascript"></script>

现在,这是一个很好的解决方法,为那些喜欢它的人提供了一定程度的分离,但我讨厌说这是为什么Index.cshtml绕过带有jQuery代码的javacript标签的答案。所以我没有回答我的问题,只是这样说它可能会帮助其他几乎全部拔掉头发的人。 。 。 。

Now, this is a nice workaround that gives some degree of separation for those that like that, but I hate saying this is the answer to why Index.cshtml was bypassing the javacript tags with jQuery code in them. So I'm not answering my question, just saying so it might help someone else who has pulled out almost all their hair. . . .

推荐答案

您使用的是剃须刀引擎吗?这就是我通常拥有的......

Are you using razor engine? This is what I usually have...

在_layout.chstml

In _layout.chstml

<head>
    @RenderSection("Head", false)
</head>

然后在您的视图中

@section Head {
    <script type="text/javascript">
        $(document).ready(function () {
            // your code goes here
        });
    </script>
}

这篇关于插入到_Layout.cshtml中的$(document).ready工作原理在插入到Index.cshtml中时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆