Jquery gets the select element and selects the text and value: 1. $ ("# select_id"). Change (function () {// code...}); // Add an event for select, triggered when one of the select events is selected 2. var checktext = $ ("# select_id"). Find ("option: Selected"). Text (); // obtain the selected text 3. var checkvalue = $ ("# select_id"). Val (); // obtain the value selected by select 4. var checkindex = $ ("# select_id"). Get (0). selectedindex; // obtain the index value selected by select 5. var maxindex = $ ("# select_id option: Last"). ATTR ("Index"); // obtain the largest index value of select. Jquery gets the select element and sets the text and value: Instance analysis: 1. $ ("# select_id"). Get (0). selectedindex = 1; // select an item whose select index value is 1 2. $ ("# select_id"). Val (4); // select an item whose value is set to 4. 3. $ ("# select_id option [TEXT = 'jquery ']"). ATTR ("selected", true); // set the select text value to jquery. Jquery adds/deletes the option entry of the select element: Instance analysis: 1. $ ("# select_id"). append ("<option value = 'value'> text </option>"); // append an option for select (drop-down) 2. $ ("# select_id "). prepend ("<option value = '0'> select </option>"); // insert an option for select (first position) 3. $ ("# select_id option: Last"). Remove (); // Delete the largest option (last) index value in select) 4. $ ("# select_id option [Index = '0']"). Remove (); // Delete the option with the index value 0 in select (the first option) 5. $ ("# select_id option [value = '3']"). Remove (); // Delete the option with value = '3' In the SELECT statement 6. $ ("# select_id option [TEXT = '4']"). Remove (); // Delete the option of '4' text = '4' in select Level 3 Classification <select name = "thirdlevel" id = "thirdlevel" Onchange = "getfourthlevel ()"> <Option value = "0" id = "thirdoption"> Select Level 3 Category </Option> </SELECT> </Div> Level 4: <Select name = "fourthlevelid" id = "fourthlevelid"> <Option value = "0" id = "fourthoption"> Select level 4 </Option> </SELECT> </Div> . If ($ ("# thirdlevel"). Val ()! = 0 ){ $ ("# Thirdlevel option [value! = 0] "). Remove (); } If ($ ("# fourthlevelid"). Val ()! = 0 ){ $ ("# Fourthlevelid option [value! = 0] "). Remove (); } // This indicates: if we want to select the third type, delete it if there is data in the fourth type. If there is no data in the fourth type, the default value is in the product. I often use Ajax later! Obtain select: Obtain the selected text in the SELECT statement: $ ("# Ddlregtype"). Find ("option: Selected"). Text (); Obtain the value selected by the SELECT statement: $ ("# Ddlregtype"). Val (); Obtain the index selected by the SELECT statement: $ ("# Ddlregtype"). Get (0). selectedindex; Set select: Set the index selected by select: $ ("# Ddlregtype"). Get (0). selectedindex = index; // index indicates the index value. Set the value selected by select: $ ("# Ddlregtype"). ATTR ("value", "normal"); $ ("# Ddlregtype"). Val ("normal "); $ ("# Ddlregtype"). Get (0). value = value; Set select selected text: VaR COUNT = $ ("# ddlregtype option"). length; For (VAR I = 0; I <count; I ++) {If ($ ("# ddlregtype"). Get (0). Options [I]. Text = text) { $ ("# Ddlregtype"). Get (0). Options [I]. Selected = true;
Break; } } $ ("# Select_id option [TEXT = 'jquery ']"). ATTR ("selected", true ); Set select option items: $ ("# Select_id"). append ("<option value = 'value'> text </option>"); // Add an option $ ("# Select_id"). prepend ("<option value = '0'> select </option>"); // insert an option $ ("# Select_id option: Last"). Remove (); // Delete the option with the largest index value $ ("# Select_id option [Index = '0']"). Remove (); // delete an option with an index value of 0 $ ("# Select_id option [value = '3']"). Remove (); // delete an option with a value of 3 $ ("# Select_id option [TEXT = '4']"). Remove (); // delete an option with a text value of 4 Clear select: $ ("# Ddlregtype"). Empty ();
Jquery obtains the following values: . Val () . Text ()
Set Value . Val ('set the value here ')
$ ("Document"). Ready (function (){ $ ("# Btn1"). Click (function (){ $ ("[Name = 'checkbox']"). ATTR ("checked", 'true'); // select all }) $ ("# Btn2"). Click (function (){ $ ("[Name = 'checkbox']"). removeattr ("checked"); // cancel all }) $ ("# Btn3"). Click (function (){ $ ("[Name = 'checkbox']: Even"). ATTR ("checked", 'true'); // select all odd numbers }) $ ("# Btn4"). Click (function (){ $ ("[Name = 'checkbox']"). Each (function () {// reselect If ($ (this). ATTR ("checked ")){ $ (This). removeattr ("checked "); } Else { $ (This). ATTR ("checked", 'true '); } }) }) $ ("# Btn5"). Click (function () {// output the selected value VaR STR = ""; $ ("[Name = 'checkbox'] [checked]"). Each (function (){ STR + = $ (this). Val () + "\ r \ n "; // Alert ($ (this). Val ()); }) Alert (STR ); }) })
|