Provides various official and user-released code examples. For code reference, you are welcome to learn about the Javascript operation select, which is a common form, the following describes several common methods for performing select operations in javascript:
// JavaScript Document
// Dynamically create select
Function createSelect ()
{
Var mySelect = document. createElement ("select ");
MySelect. id = "mySelect ";
Document. body. appendChild (mySelect );
}
// Add option
Function addOption ()
{
// Search for objects by id,
Var obj = document. getElementById ('myselect ');
// Add an option
Obj. add (new Option ("text", "value"); // This can only be valid in IE
Obj. options. add (new Option ("text", "value"); // compatible with IE and firefox
}
// Delete all option options
Function removeAll ()
{
Var obj = document. getElementById ('myselect ');
Obj. options. length = 0;
}
// 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 );
}
// 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;
// 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 ");
Select.zip (675 B download: 33 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB