Brief introduction to Option objects in drop down menu
- 2020-06-07 04:02:51
- OfStack
1. Create the Option object
1.1 var optionEle1 = document.createElement('option');
1.2 var optionEle2 = new Option(text, value, defaultSelected, selected);
2. options properties
2.1 select.options returns the collection of Option objects under the select tag
3. Empty the drop-down menu
3.1 Using for loop delete, note the dynamic change of array length
3.2 select. options. length = 0;
4. The application
<html>
<head>
<script language="javascript">
function number(){
var obj = document.getElementById("mySelect");
//obj.options[obj.selectedIndex] = new Option(" My taste ","4");// Change in the value of the currently selected one
//obj.options.add(new Option(" My taste ","4")); add 1 a option
//alert(obj.selectedIndex);// Show the serial number, option Self-Set
//obj.options[obj.selectedIndex].text = " My taste "; Change the value
//obj.remove(obj.selectedIndex); Delete function
}
</script>
</head>
<body>
<select id="mySelect">
<option> My bag </option>
<option> My laptop </option>
<option> My oil </option>
<option> My burden </option>
</select>
<input type="button" name="button" value=" View the results " onclick="number();">
</body>
</html>
1. Create select dynamically
function createSelect(){
var mySelect = document.createElement("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}
2. Add option option
function addOption(){
// According to the id Find the object,
var obj=document.getElementById('mySelect');
// add 1 An option
obj.add(new Option(" The text "," value ")); // This can only be used in IE The effective
obj.options.add(new Option("text","value")); // This is compatible with IE with firefox
}
3. Delete all options option
function removeAll(){
var obj=document.getElementById('mySelect');
obj.options.length=0;
}
4. Delete option option
function removeOne(){
var obj=document.getElementById('mySelect');
//index, To delete the sequence number of the option, take the sequence number of the currently selected option
var index=obj.selectedIndex;
obj.options.remove(index);
}
5. Get the value of option option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; // Sequence number, the sequence number of the currently selected option
var val = obj.options[index].value;
6. Get the text of the option option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; // Sequence number, the sequence number of the currently selected option
var val = obj.options[index].text;
7. Modify option option
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; // Sequence number, the sequence number of the currently selected option
var val = obj.options[index]=new Option(" New text "," The new value ");
8. Delete select
function removeSelect(){
var mySelect = document.getElementById("mySelect");
mySelect.parentNode.removeChild(mySelect);
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<head>
<script language=JavaScript>
function $(id)
{
return document.getElementById(id)
}
function show()
{
var selectObj=$("area")
var myOption=document.createElement("option")
myOption.setAttribute("value","10")
myOption.appendChild(document.createTextNode(" Shanghai "))
var myOption1=document.createElement("option")
myOption1.setAttribute("value","100")
myOption1.appendChild(document.createTextNode(" nanjing "))
selectObj.appendChild(myOption)
selectObj.appendChild(myOption1)
}
function choice()
{
var index=$("area").selectedIndex;
var val=$("area").options[index].getAttribute("value")
if(val==10)
{
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
var sh=document.createElement("select")
sh.add(new Option(" Pudong new area ","101"))
sh.add(new Option(" Huangpu district ","102"))
sh.add(new Option(" Xuhui district ","103"))
sh.add(new Option(" Putuo district ","104"))
$("context").appendChild(sh)
}
if(val==100)
{
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
var nj=document.createElement("select")
nj.add(new Option(" Xuanwu borough ","201"))
nj.add(new Option(" baixia ","202"))
nj.add(new Option(" Xiaguan district ","203"))
nj.add(new Option(" Qixia district ","204"))
$("context").appendChild(nj)
}
}
function calc()
{
var x=$("context").childNodes.length-1;
alert(x)
}
function remove()
{
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
}
</script>
<body>
<div id="context">
<select id="area" on
change="choice()">
</select>
</div>
<input type=button value=" According to " onclick="show()">
<input type=button value=" Compute nodes " onclick="calc()">
<input type=button value=" delete " onclick="remove()">
</body>
</html>
This is the end of this article, I hope you enjoy it.