简体   繁体   English

分配的参数和定义函数,jQuery

[英]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. 好的,我将$ cars数组分配给cars变量。 Basically $cars is an array of objects, each object is an associative array. 基本上$ cars是一个对象数组,每个对象都是一个关联数组。 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. 下一个功能使用汽车中的信息并将其显示在html元素中。 iNLarr is an array of functions each function changing a replacing name html elements with the name retrieved from the $cars array of objects. iNLarr是一个函数数组,每个函数都使用从$ cars对象数组中检索的名称来更改替换名称html元素。

Whenever I run the function I get something back like array.prototype.map is not a valid parameter. 每当我运行该函数时,我都会得到类似array.prototype.map的有效参数。 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. 我认为startList是我希望将其分配给对象的虚构参数,以便我可以检索每个名称,但是我猜并非如此,任何人都可以帮助我。

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 . 会使用未定义(至少在显示的代码中未定义)变量cars 覆盖 ajax调用的结果。 And then you don't actually do anything further with $cars - no point giving it a value if you don't use it. 然后,您实际上不会对$cars进行任何进一步的操作-如果您不使用它,就没有任何价值。

Then your second function is getting bound as a document ready handler which means the cars argument will be set to reference jQuery . 然后,您的第二个函数将绑定为文档就绪处理程序 ,这意味着cars参数将设置为引用jQuery That is, when you call $() or jQuery() and pass a function as an argument: 也就是说,当您调用$()jQuery()并传递一个函数作为参数时:

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

...that function will be called when the DOM is ready. ...当DOM准备就绪时,将调用该函数。

You should do all of the processing you need with your ajax call within the first function, something like this: 您应该使用第一个函数中的ajax调用来完成所需的所有处理,如下所示:

$.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. $(function () { … }符号用于在文档准备/加载时执行代码。

Your $.get is done at script loading time (begore document loaded), but the response can come after the document loading. $.get在脚本加载时完成(即加载了文档),但是响应可以在文档加载后进行。

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

May it help you. 愿它对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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