The jquery Operation Drop-down Box (DropDownList) implements the value assignment _jquery

Source: Internet
Author: User
1. Get Selected items:
Copy Code code as follows:

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:
Copy Code code as follows:

$ (' Select#sel '). Get (0). SelectedIndex;

3. Get the maximum index value for the current option:
Copy Code code as follows:

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

4. Get the length of the DropDownList:
Copy Code code as follows:

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

5. Set the first option to the selected value:
Copy Code code as follows:

$ (' Select#sel option:first '). attr (' selected ', ' true ')
Or
$ (' Select#sel ') [0].selectedindex = 0;

6. Set the last option to the selected value:
Copy Code code as follows:

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

7. Set any option to the selected value based on the index value:
Copy Code code as follows:

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

8. Set the VALUE=4 option to the selected value:
Copy Code code as follows:

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

9. Option to delete value=3:
Copy Code code as follows:

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

10. Delete the first few option:
Copy Code code as follows:

$ ("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:
Copy Code code as follows:

$ ("Select#sel option"). EQ (0). Remove ();
Or
$ ("Select#sel Option:first"). Remove ();

12. Delete last option:
Copy Code code as follows:

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

13. Delete DropDownList:
Copy Code code as follows:

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

14. Add an option after the select:
Copy Code code as follows:

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

15. Add an option before the select:
Copy Code code as follows:

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

16. Traverse option:
Copy Code code as follows:

$ (' select#sel option '). each (function (index, domele) {
Write code
});

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.