Each traversal object and array example in jquery

Source: Internet
Author: User

A universal traversal method that can be used to traverse objects and arrays. $ (). each (), 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. Return the callback function to False if you want to exit each loop

There are currently two select

?
123456789101112 计划类别: <select id="PLANTYPE"> <option value="0">-所有-</option> <option value="1">新建</option> <option value="2">续建</option> </select> 申报类型: <select id="AUDITTYPE"> <option value="0">-所有-</option> <option value="1">申报</option> <option value="2">修改</option> </select>

Use the each method to get the text value in option, that is-all-, new, continued ...

If you use only once each loop, you can start at option

?
1234 $("option").each(function(index,data){ console.info($( data ).text()); //或者console.info($(this).text()); })

You can also start at select

?
12345 $("select").each(function( index,data){ $("option", data).each(function(m,n){ console.info($(this).text()); }) })

$ ("option", data) Be sure to add data or $ ("option", this), which represents the option under this object

Otherwise, it's all option.

--------------------------Gorgeous split-line-----------------------------

Each also has a usage jquery.each (object, [callback])
Unlike the $ (). each () method of a JQuery object, this method can be used to sample any object.

Use this method to traverse the same code

?
123 $.each($("option"),function(index,data){ console.info(index+" "+data); })

You can also iterate over an array

?
123 $.each( [0,1,2], function(i, n){ console.info( "Index:"+ i + ": "+ n ); });

Traversing objects

?
123 $.each({ name: "itmyhome", addr: "Beijing"},function(i, n){ console.info("Name: " + i + ", Value: "+ n); });

 

Each traversal object and array example in jquery

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.