Uncaught ReferenceError: myFunction is not defined

前端 未结 4 678
长发绾君心
长发绾君心 2020-12-06 19:36

Gives an error. I have placed the code just before . Still getting the error.

相关标签:
4条回答
  • 2020-12-06 20:09

    From the jQuery docs:

    The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.

    So your function isn't created until after your onclick is established. Thus it can't find the function. You'll want to move it outside the $(document).ready(function(){}).

    0 讨论(0)
  • 2020-12-06 20:19

    You cannot place myFunction after the onclick. When the onclick is seen there is no definition for myFunction.

    Place the JavaScript in <head> tag. Also, move the function outside of ready().

    Like this:

    <script type="text/javascript">
    var nexturl ="";
    var lastid ="";
    var param;
    
    function myFunction() {
        param = $('#search').val();
        alert("I am an alert box!");
        if (param != "") {
            $("#status").show();
            var u = 'https://graph.facebook.com/search/?callback=&limit=100&q='+param;
            getResults(u);
            }
    }
    
    $(document).ready(function() {
    
      $("#more").click(function () { 
    
        $("#status").show();
        $("#more").hide();  
        pageTracker._trackPageview('/?q=/more');
        var u = nexturl;
        getResults(u);
      });
    
    });
    </script>
    </head>
    <body>
    ...
    
    0 讨论(0)
  • 2020-12-06 20:32

    keep myFunction in script tag directly

     i.e
       <script>
    
    function myFunction() {
      .....  
    }
     </script>
    
    0 讨论(0)
  • 2020-12-06 20:35

    You have to move the function outside the $document.ready here then you will have two options: 1st move it to <head> or 2nd move it right before the closing $document.ready bracket. Use this type of declaration for your function myFunction(){alert("inside my function");};

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