Select option object

Source: Internet
Author: User
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>

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.