JQuery Callback with arguments - newbie

So, at the bottom of jQuery's main pages , it gives this example, NOTICE of how anonymous function()has no parameters. Quote jquery page says

An anonymous function does only one thing: it calls myCallBack with the values ​​param1 and param2 in the outer scope.

$.get('myhtmlpage.html', function(){
  myCallBack(param1, param2);
});

So, I gave it a try, based on in this example

My code is similar to this, a similar thing, on line 10, I have anonymous function(result), but I need to add the parameter β€œresult” cannot be simple function()as suggested on the JQuery website, or it will not work:

<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                var GetResults = function (result) {
                        $("div").html(result);
                    }
                $("button").click(function () {
                    $.get("demo_ajax_load.txt", function (result) {
                        GetResults(result);
                    });
                });
            });
        </script>
    </head>
    <body>
        <div>
            <h2>Let AJAX change this text</h2>
        </div>
        <button>Change Content</button>
    </body>
</html>

: JQuery , function() ? function(result), ? - ?

+3
2

, :

: myCallBack param1 param2

, param1 param2 ( ):

$("button").click(function () {
    var param1 = "something";
    var param2 = "something else";
    $.get('myhtmlpage.html', function(){
       myCallBack(param1, param2); // this uses the previously declared variables
    });
});

JavaScript, myfunc("some text"), , function myfunc(var1) { function myfunc() {, ...

​function myfunc() {
    alert(arguments[0]);
}

myfunc("some text");​​​​​​​​​​

ar arguments... . MDN

+7

, . get , result, - .

: result result="Hello World"; .

+1

All Articles