Home > Web Front-end > JS Tutorial > body text

JS sample code for adding, deleting, modifying and checking options of select control options_javascript skills

WBOY
Release: 2016-05-16 17:19:31
Original
978 people have browsed it

Javascript operation select is a common type of form. Here are several commonly used methods of dynamic JS operation select:

Copy code The code is as follows:

//Dynamicly create select
function createSelect()
{
var mySelect = document.createElement("select");
mySelect .id = "mySelect";
document.body.appendChild(mySelect);
}

Copy code The code is as follows:

//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
}

Copy Code The code is as follows:

//Remove all options option
function removeAll()
{
var obj=document.getElementById(' mySelect');
obj.options.length=0;
}

Copy code The code is as follows:

//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);
}

Copy code The code is as follows:

//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;

Copy code The code is as follows:

//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 ");
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!