The value assignment implementation code of CheckBox, RadioButton, DropDownList in jquery

Source: Internet
Author: User

Since the version of jquery has been updated very quickly, the code has changed a lot, the following jquery code is suitable for query1.4 version above
Radio

1. To get the selected value, three methods are available:
$ (' input:radio:checked '). Val ();
$ ("input[type= ' Radio ']:checked"). Val ();
$ ("Input[name= ' rd ']:checked"). Val ();
2. Set the first radio to 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:last '). attr (' checked ', ' checked ');
Or
$ (' Input:radio:last '). attr (' checked ', ' true ');
4. Set any one of the radio to the selected value according to the index value:
$ (' Input:radio '). EQ (index value). attr (' checked ', ' true '); index value =0,1,2 ....
Or
$ (' Input:radio '). Slice. attr (' checked ', ' true ');
5. Set Radio to the selected value based on value
$ ("input:radio[value= ' Rd2 ')"). attr (' checked ', ' true ');
Or
$ ("input[value= ' Rd2 ')"). attr (' checked ', ' true ');
6. Remove Radio with value Rd2
$ ("input:radio[value= ' Rd2 ')". Remove ();
7. Delete the first few radio
$ ("Input:radio"). EQ (index value). Remove (); index value =0,1,2 ....
such as removing the 3rd radio:$ ("Input:radio"). EQ (2). Remove ();
8. Traverse Radio
$ (' Input:radio '). each (function (Index,domele) {
Write code
});

DropDownList

1. Get the check:
Gets the value of the selected item:
$ (' Select#sel option:selected '). Val ();
Or
$ (' Select#sel '). Find (' option:selected '). Val ();
Gets the text value of the selected item:
$ (' select#seloption:selected '). Text ();
Or
$ (' Select#sel '). Find (' option:selected '). Text ();
2. Gets the index value of the currently selected item:
$ (' Select#sel '). Get (0). SelectedIndex;
3. Get the maximum index value for the current option:
$ (' Select#sel option:last '). attr ("index")
4. Get the length of the DropDownList:
$ (' Select#sel ') [0].options.length;
Or
$ (' Select#sel '). Get (0). Options.length;
5. Set the first option to a 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 to the selected value according to the index value:
$ (' Select#sel ') [0].selectedindex = index value; index value =0,1,2 ....
8. Set option for Value=4 to the selected value:
$ (' Select#sel '). attr (' Value ', ' 4 ');
Or
$ ("Select#sel option[value= ' 4 ']"). attr (' selected ', ' true ');
9. Delete the value=3 option:
$ ("Select#sel option[value= ' 3 ']"). Remove ();
10. Delete the first few option:
$ ("Select#sel option"). EQ (index value). Remove (); index value =0,1,2 ....
If you delete a 3rd radio:
$ ("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 the select:
$ ("Select#sel"). Append ("<option value= ' 6 ' >f</option>");
15. Add an option in front of select:
$ ("Select#sel"). Prepend ("<option value= ' 0 ' >0</option>");
16. Traverse option:
$ (' select#sel option '). each (function (index, domele) {
Write code
});

CheckBox

1. Get a single checkbox check (three ways):
$ ("input:checkbox:checked"). Val ()
Or
$ ("input:[type= ' checkbox ']:checked"). Val ();
Or
$ ("input:[name= ' ck ']:checked"). Val ();
2. Get multiple checkbox-checked items:
$ (' Input:checkbox '). each (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 the 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. attr (' checked ', ' true ');
6. Select multiple checkboxes:
Check both the 1th and 2nd checkboxes:
$ (' Input:radio '). Slice (0,2). attr (' checked ', ' true ');
7. Set the checkbox to the selected value based on value:
$ ("input:checkbox[value= ' 1 ']"). attr (' checked ', ' true ');
8. Delete the Value=1 checkbox:
$ ("input:checkbox[value= ' 1 ']"). Remove ();
9. Delete the first few checkboxes:
$ ("Input:checkbox"). EQ (index value). Remove (); index value =0,1,2 ....
To delete a 3rd checkbox:
$ ("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. Cancel All selections:
$ (' Input:checkbox '). each (function () {
$ (this). attr (' checked ', false);
});

The value assignment implementation code of CheckBox, RadioButton, DropDownList 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.