The Javascript action Select is a common form in forms, and the following are some of the most commonly used methods of JS dynamic operation select:
Copy Code code as follows:
Create a Select dynamically
function Createselect ()
{
var myselect = document.createelement ("select");
Myselect.id = "Myselect";
Document.body.appendChild (Myselect);
}
Copy Code code as follows:
Add Options option
function AddOption ()
{
To find an object based on its ID,
var Obj=document.getelementbyid (' Myselect ');
Add an option
Obj.add (New Option ("text", "value"); This only works in IE.
Obj.options.add (New Option ("text", "value")); This compatible with IE and Firefox
}
Copy Code code as follows:
Delete all options option
function RemoveAll ()
{
var Obj=document.getelementbyid (' Myselect ');
obj.options.length=0;
}
Copy Code code as follows:
Delete an option
function Removeone ()
{
var Obj=document.getelementbyid (' Myselect ');
Index, to delete the ordinal number of the option, take the number of the currently selected option
var Index=obj.selectedindex;
Obj.options.remove (index);
}
Copy Code code as follows:
To get the text of options option
var Obj=document.getelementbyid (' Myselect ');
var Index=obj.selectedindex; Ordinal number, taking the number of the currently selected option
var val = obj.options[index].text;
Copy Code code as follows:
//Modify Options Option
Var obj=document.getelementbyid (' Myselect ');
Var index=obj.selectedindex//ordinal, taking the ordinal number of the currently selected option
var val = obj.options[index]=new option ("New text", "New value");