简体   繁体   English

如何在jquery $ .each(…)中而不是内联中调用单独的javascript函数?

[英]How to call a separate javascript function in jquery $.each(…) instead of inline?

I have a simple iteration using jQuery each each都有一个使用jQuery的简单迭代

 $.each(data.Limit, function (index, value) {
   alert(value.LimitCategory);
 });

How do I change this to call an actual javascript function - which I want to maintain separately from the loop? 如何将其更改为调用实际的javascript函数-我想与循环分开维护该函数?

eg 例如

function DoSomethingElse(index, value){
//more things here
};

I've tried a few ways and can't seem to get the syntax. 我尝试了几种方法,但似乎无法获取语法。

只需将函数作为each方法的第二个参数传递即可:

$.each(data.Limit, DoSomethingElse)

Simply declare the needed method and call it. 只需声明所需的方法并调用它。

 $.each(data.Limit, function (index, value) {
   someMethod(index, value);
 });

 function someMethod(i, v){
   // process
 }

So when you pass in an anonymous function as the second parameter of $.each you ARE ACTUALLY CALLING AN actual JAVASCRIPT FUNCTION (sorry about the caps my phone is not behaving well.) in case you wanna define the function and pass in a function name, the syntax would be as follows. 因此,当您传入匿名函数作为$。$的第二个参数时,您实际上是在调用一个实际的JAVASCRIPT函数(对不起,我的手机的表现不佳。),以防您想定义函数并传入函数名,语法如下。

function myFunction (i, val) {
    //i is for index
    //use this or val for current object in array
}

$.each(array, myFunction);

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

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