| I. Basic understanding: var e = document. getElementById ("selectId"); e. options = new Option ("text", "value"); // create an option object, create one or more <option value = "value"> text </option> In the <select> label. Options is an array that contains multiple <option value = "value"> text </option> labels. 1. options array attribute: length ------- length attribute selectedIndex ------ index value of the selected text, which is automatically allocated by the memory (, 2, 3 ....) (The first text value, the second text value, the third text value, and the fourth text value .......) 2. attributes of a single option (obj. options [obj. selectedIndex] is a specified <option> label): text ==== return/specify text value ==== return/specify text, and <option value = "... "> consistent index ====== returns the subscript selected ==== returns/specifies whether the object is selected, if you specify true or false, You can dynamically change the selected defaultSelected ==== to return whether the object is selected by default. The true/false3 and option methods are as follows: add a <option> label ====== obj. options. add (new ("text", "value") deletes a <option> label ====== obj. options. remove (obj. selectedIndex); obtain a <option> label ====== obj. options [obj. selectedIndex]. text; modify a <option> label ====== obj. options [obj. selectedIndex] = new Option ("new text", "value"); Delete all <option> labels ==== obj. options. length = 0; obtain the value of a <option> tag === obj. options [obj. selectedIndex]. value; Note: obj. the option in option does not need to be capitalized. The Option in new option needs to be capitalized /////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////// 1. Check whether selected If (objSelect. selectedIndex>-1 ){ // Indicates the selected } Else { // The description is not selected. } 2. dynamically create select Function createSelect (){ Var mySelect = document. createElement ("select "); MySelect. id = "mySelect "; Document. body. appendChild (mySelect ); } 3. Add option Function addOption (){ // Search for objects by id, Var obj = document. getElementById ('myselect '); // Add an option Obj. add (new Option ("text", "value ")); } 4. Delete All option options Function removeAll (){ Var obj = document. getElementById ('myselect '); Obj. options. length = 0; } 5. delete an option Function removeOne (){ Var obj = document. getElementById ('myselect '); // Index. The sequence number of the option to be deleted. The sequence number of the selected option is used here. Var index = obj. selectedIndex; Obj. options. remove (index ); } 6. Obtain the option value. Var obj = document. getElementById ('myselect '); Var index = obj. selectedIndex; // the serial number of the selected option. Var val = obj. options [index]. value; 7. Obtain the option text Var obj = document. getElementById ('myselect '); Var index = obj. selectedIndex; // the serial number of the selected option. Var val = obj. options [index]. text; 8. Modify option Var obj = document. getElementById ('myselect '); Var index = obj. selectedIndex; // the serial number of the selected option. Var val = obj. options [index] = new Option ("new text", "new Value "); 9. Delete select Function removeSelect (){ Var mySelect = document. getElementById ("mySelect "); MySelect. parentNode. removeChild (mySelect ); } |