Operations on the jquery Select Control

Source: Internet
Author: User
Jquery's methods for obtaining and setting Select Options are summarized as follows: Get select first, and look at the following code: $ ("# select_id "). change (function () {// code ...}); // Add an event for select. When one of the select events is selected, VAR checktext =$ ("# select_id") is triggered "). find ("option: Selected "). text (); // obtain the textvar checkvalue selected by select = $ ("# select_id "). val (); // obtain the valuevar checkindex selected by select = $ ("# select_id "). get (0 ). selectedindex; // obtain the selected index value var maxindex =$ ("# select_id option: Last "). ATTR ("Index"); // obtain the maximum index value of select $ ("# select_id "). get (0 ). selectedindex = 1; // select $ ("# select_id") for items whose select index value is 1 "). val (4); // select a value of 4 from select $ ("# select_id option [TEXT = 'jquery ']"). ATTR ("selected", true); // select an item whose select text value is jquery
Obtain the selected text: $ ("# ddlregtype"). Find ("option: Selected"). Text ();
Obtain the value selected by the SELECT statement: $ ("# nowamagic"). Val ();
Obtain the index selected by select: $ ("# nowamagic"). Get (0). selectedindex;
Set selectjquery to add/delete select option items: $ ("# select_id "). append ("<option value = 'value'> text </option>"); // append an option for select (drop-down) $ ("# select_id "). prepend ("<option value = '0'> select </option>"); // insert an option for select (first position) $ ("# select_id option: last "). remove (); // Delete the maximum option (last) of the index values in the SELECT statement $ ("# select_id option [Index = '0']"). remove (); // delete option (first) with the index value 0 in select $ ("# select_id option [value = '3']"). remove (); // delete option $ ("# select_id option [TEXT = '4']") of value = '3' in select. remove (); // Delete the option of '4' in the SELECT statement and set the selected index in the SELECT statement: // The index is the index value $ ("# nowamagic "). get (0 ). selectedindex = index;
Set the value selected by select: $ ("# nowamagic "). ATTR ("value", "normal"); $ ("# nowamagic "). val ("normal"); $ ("# nowamagic "). get (0 ). value = value;
Set select selected text: var COUNT = $ ("# nowamagicoption"). length; For (VAR I = 0; I <count; I ++ ){
If ($ ("# nowamagic "). get (0 ). options [I]. TEXT = text) {$ ("# nowamagic "). get (0 ). options [I]. selected = true; break ;}}
Clear select: $ ("# nowamagic"). Empty ();

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.