Jquery CheckBox, RadioButton, DropDownList value Assignment code

Source: Internet
Author: User

The following jquery code is suitable for query1.4 version.

1. Get the selected value, three methods can be:

$ (' 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 to the selected value:

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

Or

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

4. Set any one radio to 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 to selected value according to value

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

Or

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

6. Delete the radio value of 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 delete 3rd radio:$ ("Input:radio"). EQ (2). Remove ();

8. Traverse Radio

$ (' Input:radio '). each (function (Index,domele) {

Write code

});

DropDownList


1. Get Selected items:

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 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 to the selected value based on the index value:

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

8. Set the VALUE=4 option to the selected value:

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

Or

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

9. Option to delete value=3:

$ ("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 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 before the 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 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 checked:

$ (' 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 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 checkbox:

Select both the 1th and 2nd checkbox:

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

7. Set checkbox to selected value based on value:

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

8. Delete the checkbox for 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. Cancel all options:

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

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

});

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.