怎样理解jquery中常用的匿名函数的参数传递问题。

var arr = new Array();
$("input[name=names]:checked").each(
function(a,c){
arr[a] = $(c).val();}
这是一段juery的代码,意思是取所有的name=names的已勾选的复选框的值放到arr数组里面。我想问的是each里的function的(a,c)参数是哪里传过来的,这样写是什么意思。

object.each( function(index, Element) )
The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.
这个匿名函数是each方法中的回调函数,前面的jQuery object是一个数组,each方法会对数组中子元素的逐个进行回调函数调用,直至调用某个子元素返回的结果为false为止。参数是由each方法传给回调函数的。你那段代码的意思是把$("input[name=names]:checked")数组中的数值复制到数组arr中。追问

each传进来的a,b就是数组的key和value?

追答

a是数组的index,一般是数值;
c是DOM Element,是$("input[name=names]:checked")数组中的一项,用val()取的才是数值

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答