Value assignment for CheckBox, RadioButton, and DropDownList in Jquery

Source: Internet
Author: User
As Jquery becomes more and more useful, more and more friends are using it. In the Web, because the CheckBox, Radiobutton, DropDownList, and other controls are frequently used, they are related to the operations of these controls in Jquery. Because Jquery is updated quickly, the code is written... SyntaxHighligh

As Jquery becomes more and more useful, more and more friends are using it. In the Web, because the CheckBox, Radiobutton, DropDownList, and other controls are frequently used, they are related to the operations of these controls in Jquery. The Jquery version is updated quickly, and the code writing has changed a lot. The following Jquery Code is applicable to query1.4 and later versions.

 

Radio



1. To obtain the selected value, you can use either of the following methods:

$ ('Input: radio: checked'). val ();

$ ("Input [type = 'Radio ']: checked"). val ();

$ ("Input [name = 'rd']: checked"). val ();

2. Set the first Radio as the selected value:

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

Or

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

Note: attr ("checked", 'checked') = attr ("checked", 'true') = attr ("checked", true)

3. Set the last Radio as the selected value:

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

Or

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

4. Set any radio as the selected value based on the index value:

$ ('Input: radio '). eq (index value). attr ('checked', 'true'); index value = 0, 1, 2 ....

Or

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

5. Set Radio as the selected Value based on the Value.

$ ("Input: radio [value = 'rd2 ']"). attr ('checked', 'true ');

Or

$ ("Input [value = 'rd2 ']"). attr ('checked', 'true ');

6. Delete Radio whose Value is rd2.

$ ("Input: radio [value = 'rd2 ']"). remove ();

7. Delete the Radio numbers

$ ("Input: radio"). eq (index value). remove (); index value = 0, 1, 2 ....

For example, delete 3rd Radio: $ ("input: radio"). eq (2). remove ();

8. Traverse Radio

$ ('Input: radio '). each (function (index, domEle ){

// Write code

});

 

 

DropDownList

 


 

1. Obtain the selected items:

Obtain the Value of the selected item:

$ ('Select # sel option: selected'). val ();

Or

$ ('Select # sel '). find ('option: selected'). val ();

Obtain the Text value of the selected item:

$ ('Select # seloption: selected'). text ();

Or

$ ('Select # sel '). find ('option: selected'). text ();

2. Obtain the index value of the selected item:

$ ('Select # sel '). get (0). selectedIndex;

3. obtain the maximum index value of the current option:

$ ('Select # sel option: la'). attr ("index ")

4. Get the DropdownList length:

$ ('Select # sel ') [0]. options. length;

Or

$ ('Select # sel '). get (0). options. length;

5. Set the first option to the selected value:

$ ('Select # sel option: first '). attr ('selected', 'true ')

Or

$ ('Select # sel ') [0]. selectedIndex = 0;

6. Set the last option to the selected value:

$ ('Select # sel option: last). attr ('selected', 'true ')

7. set any option as the selected value based on the index value:

$ ('Select # sel ') [0]. selectedIndex = index value; index value = 0, 1, 2 ....

8. Set option of Value = 4 to the selected Value:

$ ('Select # sel '). attr ('value', '4 ');

Or

$ ("Select # sel option [value = '4']"). attr ('selected', 'true ');

9. delete option with Value = 3:

$ ("Select # sel option [value = '3']"). remove ();

10. Delete the following options:

$ ("Select # sel option"). eq (index value). remove (); index value = 0, 1, 2 ....

For example, delete 3rd Radio entries:

$ ("Select # sel option"). eq (2). remove ();

11. Delete the first option:

$ ("Select # sel option"). eq (0). remove ();

Or

$ ("Select # sel option: first"). remove ();

12. Delete the last option:

$ ("Select # sel option: last"). remove ();

13. Delete dropdownlist:

$ ("Select # sel"). remove ();

14. Add an option after select:

$ ("Select # sel"). append (" F");

15. Add an option before select:

$ ("Select # sel"). prepend ("0");

16. Traverse option:

$ ('Select # sel option '). each (function (index, domEle ){

// Write code
});

 

 

CheckBox

1. Obtain the selected items of a single checkbox (three methods ):

$ ("Input: checkbox: checked"). val ()

Or

$ ("Input: [type = 'checkbox']: checked"). val ();

Or

$ ("Input: [name = 'ck ']: checked"). val ();

2. Obtain multiple checkbox selected items:

$ ('Input: checkbox'). each (function (){

If ($ (this). attr ('checked') = true ){

Alert ($ (this). val ());

}

});

Or

$ ('Input: checkbox'). map (function (){
Return $ (this). val ();
}). Get (). join (',');

3. Set the first checkbox as the selected value:

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

Or

$ ('Input: checkbox'). eq (0). attr ("checked", 'true ');

4. Set the last checkbox as the selected value:

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

Or

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

5. set any checkbox as the selected value based on 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 checkboxes:

Select 1st and 2nd checkboxes at the same time:

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

7. Set checkbox as the selected Value based on the Value:

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

8. Delete the checkbox with Value = 1: www.2cto.com

$ ("Input: checkbox [value = '1']"). remove ();

9. Delete the following checkboxes:

$ ("Input: checkbox"). eq (index value). remove (); index value = 0, 1, 2 ....

For example, delete 3rd checkboxes:

$ ("Input: checkbox"). eq (2). remove ();

10. traverse the checkbox:

$ ('Input: checkbox'). each (function (index, domEle ){

// Write code
});

 

11. Select All

$ ('Input: checkbox'). each (function (){

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

});

12. deselect all options:

$ ('Input: checkbox'). each (function (){

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

});

Author: Zhu Meimei

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.