jquery Action Rollup for check box (checkbox) _jquery

Source: Internet
Author: User

jquery Action check box (checkbox) 12 tips.

1. Get a single checkbox selection (three ways to type)

$ ("input:checkbox:checked"). Val ()

Or

$ ("input:[type= ' checkbox ']:checked"). Val ();

Or

$ ("input:[name= ' ck ']:checked"). Val ();

2. Get multiple checkbox selected items

$ (' Input:checkbox '). each (the function () {
if ($ (this). attr (' checked ') ==true) {
alert ($ (this). Val ());
}
);

3. Set the first checkbox to the selected value

$ (' Input:checkbox:first '). attr ("Checked", ' checked ');

Or

$ (' Input:checkbox '). EQ (0). attr ("Checked", ' true ');

4. Set the last checkbox to a selected value

$ (' Input:radio:last '). attr (' checked ', ' checked ');

Or

$ (' Input:radio:last '). attr (' checked ', ' true ');

5. Set any checkbox to the selected value according to the index value

$ (' Input:checkbox). EQ (index value). attr (' checked ', ' true ');

Index value =0,1,2 ....
Or

$ (' Input:radio '). Slice (1,2). attr (' checked ', ' true ');

6, select multiple checkbox and select both the 1th and 2nd checkbox

$ (' Input:radio '). Slice (0,2). attr (' checked ', ' true ');

7. Set checkbox to selected value according to value

$ ("input:checkbox[value= ' 1 ']"). attr (' checked ', ' true ');

8, delete the checkbox value=1

$ ("input:checkbox[value= ' 1 ']"). Remove ();

9, delete the first few checkbox

$ ("Input:checkbox"). EQ (index value). Remove ();

Index value =0,1,2 ....
If you delete a 3rd checkbox:

$ ("Input:checkbox"). EQ (2). Remove ();

10. Traverse checkbox

$ (' Input:checkbox '). each (function (index, domele) {
//write Code
});

11, All selected

$ (' Input:checkbox '). each (function () {
$ (this). attr (' checked ', true);
});

12, all cancel the selection

$ (' Input:checkbox '). each (function () {
$ (this). attr (' checked ', false);
});

Some related operations of jquery on a checkbox

First, select the checkbox by selector:

1. Set an id attribute to the checkbox and select it by ID selector:

  <input type= "checkbox" Name= "Mybox" id= "Chkone" value= "1" checked= "checked"/>

Jquery:

    $ ("#chkOne"). Click (function () {});

2. Set a class attribute to the checkbox, selected by the class selector:

  <input type= "checkbox" Name= "Mybox" class= "Chktwo" value= "1" checked= "checked"/>

Jquery:

    $ (". Chktwo"). Click (function () {});
 

3. Select by Tag Selector and property selector:

  <input type= "checkbox" Name= "Somebox" value= "1" checked= "checked" "/> <input type="
  checkbox "Name=" Somebox "value=" 2 "/>

Jquery:

    $ ("Input[name= ' Somebox ']"). Click (function () {});
 

Ii. operation of the checkbox:
Take this checkbox code as an example:

  <input type= "checkbox" name= "box" value= "0" checked= "checked"/> "<input" type= "
  checkbox" name= "box" value= "1"/>
  <input type= "checkbox" name= "box" value= "2"/> <input type= "
  checkbox" name= "box" value= "3"/ >

1. Traverse checkbox with each () method:

    $ ("Input[name= ' box ']"). each (function () {});

2. Set checkbox to be selected with attr () method:

   $ ("Input[name= ' box ']"). attr ("Checked", "checked");
 

In HTML, if a check box is selected, the corresponding tag is checked= "checked". But if you use jquery alert ($ ("#id"). attr ("Checked"), you will be prompted to be "true" instead of "checked", so judge if ("Checked" ==$ ("#id"). attr ("checked") Is wrong, should be if (true = = $ ("#id"). attr ("checked")

3. Get the value of the selected checkbox:

  $ ("Input[name= ' box '][checked]"). each (function () {
  if (true = = $ (this). attr ("checked")) {
     alert ($ (this). attr (' value ');
  }

Or:

  $ ("Input[name= ' box ']:checked"). each (function () {
  if (true = = $ (this). attr ("checked")) {
     alert ($ (this). attr ( ' value ');
  }

$ ("Input[name= ' box ']:checked") and $ ("Input[name= ' box") "What's the difference?" I tried using $ ("Input[name= ' box") to succeed.
4. Get the value of the unchecked checkbox:

  $ ("Input[name= ' box ']"). each (function () {
     if ($ (this). attr (' checked ') ==false) {
        alert ($ (this). Val ());
      }
   );

5. Set the value of the checkbox's Values property:

     $ (this). attr ("value", values);

Third, the general is to create a JS array to store the value of the Traversal checkbox to create a JS array method:

1. var array= new Array ();

2. Add data to the array:

Array.push ($ (this). Val ());

3. Array with "," separate output:

Alert (Array.join (', '));

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.