简体   繁体   中英

Assigned parameters and defining functions, jQuery

Right now I have the code:

$.get("test.php", function(($cars){
    $cars = cars;
}, "json");

$(function(cars){
    $.each(cars, function(i, startList){
        if(iNLarr[i]){
            iNLarr[i](startList.name);
        }
    });
});

Ok I'm assigning the $cars array to the cars variable. Basically $cars is an array of objects, each object is an associative array. I need to be able to work on the array so I assigned it to the variable first. The next function uses the information in cars and displays it in html elements. iNLarr is an array of functions each function changing a replacing name html elements with the name retrieved from the $cars array of objects.

Whenever I run the function I get something back like array.prototype.map is not a valid parameter. I am thinking that startList is a made up parameter that I was hoping would be assigned to the objects so I could retrieve each name, but I'm guessing that's not the case, can anyone help me.

I'm alos not sure I've defined the function properly.

Use the below code to accomplish your task :

$.get("test.php", function(($cars){
    cars = $cars;
    $.each(cars, function(i, startList){
        if(iNLarr[i]){
            iNLarr[i](startList.name);
        }
    });
}, "json");

Tell me if need any explanation.

At the moment neither function makes sense. This line in the first:

$cars = cars;

will overwrite the result of your ajax call with an undefined (at least, undefined in the code shown) variable cars . And then you don't actually do anything further with $cars - no point giving it a value if you don't use it.

Then your second function is getting bound as a document ready handler which means the cars argument will be set to reference jQuery . That is, when you call $() or jQuery() and pass a function as an argument:

$(function() { /* some code */ });

...that function will be called when the DOM is ready.

You should do all of the processing you need with your ajax call within the first function, something like this:

$.get("test.php", function(cars){
    $.each(cars, function(i, startList){
        if(iNLarr[i]){
            iNLarr[i](startList.name);
        }
    });
}, "json");

You may need to wrap the above in a document ready handler, or put it in a script at the end of the body.

The $(function () { … } notation is for executing code at document ready/load time.

Your $.get is done at script loading time (begore document loaded), but the response can come after the document loading.

$(function() {
    $.get("test.php", function(cars) {
        $.each(cars, function(i, startList) {
            if(iNLarr[i]){
                iNLarr[i](startList.name);
            }
        });
    }, "json");
});

May it help you.

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