Using ript to operate option select

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. Properties of the options array: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> tag ):Text ===== return/specify text value ==== return/specify text, with <option value = "... "> consistent index ====== returns the subscript selected ==== returns/specifies whether the object is selected, if true or false is specified, the selected defaultselected =====returned to check whether the selected 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. options in option do not need to be capitalized. Options in new option must 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 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 );
}

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.