Sample array, using both the element index and content. (I is the index, n is the content)
CopyCode The Code is as follows: $. Each ([0, 1, 2], function (I, n ){
Alert ("item #" + I + ":" + n );
});
Sample object and use both the member name and variable content. (I is the member name, and N is the variable content)Copy codeThe Code is as follows: $. Each ({name: "John", Lang: "JS"}, function (I, n ){
Alert ("name:" + I + ", value:" + n );
});
This example uses an input form element as an example.
If you have such code in the Dom
<Input name = "AAA" type = "hidden" value = "111"/>
<Input name = "BBB" type = "hidden" value = "222"/>
<Input name = "CCC" type = "hidden" value = "333"/>
<Input name = "DDD" type = "hidden" value = "444"/>
Then you use the each as follows:Copy codeThe Code is as follows: $. each ($ ("input: hidden"), function (I, Val ){
Alert (VAL); // output [object htmlinputelement] because it is a form element.
Alert (I); // The output index is 0, 1, 2, 3.
Alert (Val. Name); // output the value of name
Alert (Val. Value); // output value
});
The following is an official explanation:
Jquery. Each (object, [callback])
Overview
The general example method can be used for example object and array.
Unlike the $ (). Each () method of the jquery object, this method can be used to sample any object. The callback function has two parameters: the first is the object's member or array index, and the second is the corresponding variable or content. If you want to exit the each loop, the callback function returns false. Other return values are ignored.
Parameters
Objectobject
Objects or arrays that need to be repeated.
Callback (optional) Function
The callback function executed by each member/element.