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. True/false 3. Option Method: 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. option in option does not need to be capitalized. Option in new option must be capitalized. Example 2: <HTML>
<Head>
<Script language = "JavaScript">
Function chk (){
VaR OBJ = Document. getelementbyid ("myselect ");
OBJ. Options [obj. selectedindex] = New Option ("My tests", "4"); // change the selected text
// Obj. Options. Add (New Option ("My tests", "4"); // Add another option
// Alert (obj. selectedindex); // displays the sequence number.
// Obj. Options [obj. selectedindex]. Text = "My tests"; // change the value
// Obj. Remove (obj. selectedindex); delete one by one
// Obj. Options. Length = 0; // delete all
// T = obj. Options [obj. selectedindex]. Text; // get text
// Alert (t );
// V = obj. Options [obj. selectedindex]. value; // obtain the value of the selected text.
// Alert (v );
}
</SCRIPT>
</Head>
<Body>
<Select id = "myselect">
<Option value = "1111"> my 1111 </option>
<Option value = "2222"> my 2222 </option>
<Option value = "3333"> my 3333 </option>
<Option value = "4444"> my 4444 </option>
</SELECT>
<Input type = "button" name = "button" value = "view result" onclick = "javascript: chk ();">
</Body>
</Html>