1. dynamically create select
Copy codeThe Code is as follows:
Function createSelect (){
Var mySelect = document. createElement_x ("select ");
MySelect. id = "mySelect ";
Document. body. appendChild (mySelect );
}
2. Add option
Copy codeThe Code is as follows:
Function addOption (){
// Search for objects by id,
Var obj = document. getElementByIdx_x ('myselect ');
// Add an option
Obj. add (new Option ("text", "value "));
}
3. Delete All option options
Copy codeThe Code is as follows:
Function removeAll (){
Var obj = document. getElementByIdx_x ('myselect ');
Obj. options. length = 0;
}
4. delete an option
Copy codeThe Code is as follows:
Function removeOne (){
Var obj = document. getElementByIdx_x ('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 );
}
5. Obtain the option value.
Copy codeThe Code is as follows:
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index]. value;
6. Obtain the option text
Copy codeThe Code is as follows:
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index]. text;
7. Modify option
Copy codeThe Code is as follows:
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index] = new Option ("new text", "new Value ");
8. Delete select
Copy codeThe Code is as follows:
Function removeSelect (){
Var mySelect = document. getElementByIdx_x ("mySelect ");
MySelect. parentNode. removeChild (mySelect );
}