Usage of jquery. Each ()

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.