Operations of jQuery on arrays and objects in checkbox

Source: Internet
Author: User

Bytes ------------------------------------------------------------------------------------------

In this example, jQuery selects, cancels, and reselect all the checkboxes in the form.

------------------------- @ Chenwei <www. chenwei. ws> ---------------------------

<Input type = "checkbox" class = "all"/>

<Input type = "checkbox" name = "id []" value = "1"/>

<Input type = "checkbox" name = "id []" value = "2"/> B

<Input type = "checkbox" name = "id []" value = "3"/> c

<Button class = "selectInverse"> invert selection </button>

<Script>

$ ('. All'). toggle (

Function ({

$ (": Input [name = id []"). each (function (){

$ (This). attr ('checked', true );

});

}),

Function ({

$ (": Input [name = id []"). each (function (){

$ (This). attr ('checked', false );

});

})

);

 

$ ('. Selectinverse'). click (function (){

$ (": Input [name = id []"). each (function (){

$ (This). attr ('checked ',! This. checked );

});

});

</Script>

Bytes -----------------------------------------------------------------------------------------

The above event is used: click, event switching: toggle, form Element matching: input, set the attribute of the element: attr, array and object operation: each;

$ (). Each (); used to traverse jQuery objects;

$. Each (object, [callback]) is a universal jQuery Traversal method that can be used to traverse objects and arrays;

Example:

1. traverse the array and use the element index and content at the same time.

$. Each ([0, 1, 2], function (I, n ){

Console. log ('item: '+ I +', value: '+ n );

});

2. traverse the object and use both the member name and variable content

$. Each ({name: 'chenwei', age: '81 '}, function (I, n ){

Console. log ('name: '+ I +', age: '+ n );

});

Bytes -----------------------------------------------------------------------------------------

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.