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 ();