简体   繁体   中英

$ is not defined for anonymous function

I'm sure that my jquery script include is there and so jquery has been pulled in but I for some reason get ReferenceError: $ is not defined for $(function ().

It was working fine but then at some point it started not to recognize jquery and I don't see what could be causing that if jquery is loaded. I don't think I need a document.ready() do I?

Honestly I don't even know if I should be using an anonymous function here. I forget why I have it in the first place because it's just wrapping a bunch of functions inside, I'm basically wrapping all my js and jquery code within it for the page.

  <script src="~/Scripts/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">

        var creditCardType = 1;
        var cVV2 = 123;


        //$ is a shorthand for 'jQuery'. So $(function(){}); is a jquery function
        $(function ()
        {
            hideAllTransactionSections();
            setEvents();

            function hideAllTransactionSections() {
                $("#preAuthenticationContent").hide();
                $("#delayCaptureContent").hide();
                $("#captureContent").hide();
                $("#voidContent").hide();
                $("#refundContent").hide();
            }
           ... rest of js code for the page
       });

Not sure why I get this error still.

This means that the jQuery library not being found. Check to make sure that you src path is correct.

<script src="~/Scripts/jquery-2.0.3.min.js"></script>

You could also have a syntax error further down in the file, you might need to provide the full js code.

Check whether you have given right path . ie ur JS file is loaded on page

or else do use below link

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>

Bu using above JS , if still you getting error check your console page your error msg

Since you (more or less) asked, $(function (){}) is shorthand for $(document).ready(function (){}) . You add that so none of the jQuery code inside that wrapper executes until the DOM is ready.

As for why it's undefined, the two most common problems are

  1. Path is not valid (you should see a 404 error in the console)
  2. Another script has already mapped to the $ operator

You haven't tagged this as an ASP.NET MVC question, but if it is, you have to be using MVC4 or higher to get automatic mapping of the virtual directory (from the ~ character). Otherwise, you need to use the @Url.Content() helper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM