Javascript operation select is a common type of form. Here are several commonly used methods of dynamic JS operation select:
//Dynamicly create select
function createSelect()
{
var mySelect = document.createElement("select");
mySelect .id = "mySelect";
document.body.appendChild(mySelect);
}
//Add option option
function addOption()
{
//Find the object based on id,
var obj=document .getElementById('mySelect');
//Add an option
obj.add(new Option("text","value")); //This is only valid in IE
obj. options.add(new Option("text","value")); //This is compatible with IE and firefox
}
//Remove all options option
function removeAll()
{
var obj=document.getElementById(' mySelect');
obj.options.length=0;
}
//Remove an option option
function removeOne()
{
var obj=document.getElementById('mySelect');
// index, to delete the serial number of the option, here take the serial number of the currently selected option
var index=obj.selectedIndex;
obj.options.remove(index);
}
//Get the text of option
var obj=document. getElementById('mySelect');
var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option
var val = obj.options[index].text;
//Modify option option
var obj=document.getElementById ('mySelect');
var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option
var val = obj.options[index]=new Option("new text","new value ");