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 ");
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn