The use of $.each

Source: Internet
Author: User

$.each (Array, [callback]) traversal [common]

Explanation: Different from 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 index of the object's member or array, and the second is the corresponding variable or content. If you need to exit each loop so that the callback function returns false, the other return values are ignored.

Each traversal, I believe, is not strange, in the ordinary event processing, is a variant of the For loop, but more powerful than for loop. In the array, it can easily take the array index and the corresponding value. Cases:

The code is as follows Copy Code

var _mozi=[' mohist ', ' Mozi ', ' Modi ', ' and love non-offensive ', ' still with Shangxian ']; The array used in this article, the same as

$.each (_mozi,function (key,val) {

The callback function has two parameters, the first is an element index, the second is the current value

Alert (' _mozi array, index: ' +key+ ' corresponds to the value: ' +val ');

});

For: relative to the native. In,each a little stronger. For: In can also iterate through the array and return the corresponding index, but the value needs to be obtained by Arrname[key]




var types = new Array ();
$ ("Input[name= ' Stypecheckbox ']:checked"). each (function (key,value) {
Types[key] = $ (this). Val ();
});


Key is index, value is

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.