asp.net mvc4 jquery not working

前端 未结 3 1437
无人共我
无人共我 2021-02-18 20:55

I\'m trying to run a jquery code which was put in my _Layout.cshtml as below:

...............


        
相关标签:
3条回答
  • 2021-02-18 21:24

    In order for jQuery to work, your script should come after <script src="/Scripts/jquery-2.0.3.js"></script>

    In the _Layout.cshtml, just before the </body> tag you can find something like this

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
    

    First you have the jQuery library. Then you have any scripts. That's what this means. So in your views do something like this.

    @section scripts{
     //your script
    }
    

    This will make sure that all scripts comes under jQuery reference.

    0 讨论(0)
  • 2021-02-18 21:26

    You need to introduce jquery before your script.

    @Scripts.Render("~/bundles/jquery")
    
    <script type='text/javascript'>
    $(document).ready(function () {
       alert('Test');
    
    });
    </script>
    
      @RenderSection("Scripts", required: false) 
    </body>
    </html>
    
    0 讨论(0)
  • 2021-02-18 21:40

    Andreas, i have the same issue.. for default the "_layout.cshtml" has "@Scripts.Render("~/bundles/jquery")" at the end of the document, but it doesn't work.. i cut it and paste it in the head and now it is working.

    0 讨论(0)
提交回复
热议问题