1. Dynamically create select
Copy the code code as follows:
function createSelect(){
var mySelect = document.createElement_x("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}
2. Add option option
Copy the code code as follows:
function addOption(){
//Find objects based on id,
var obj=document.getElementByIdx_x('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
}
3. Delete all options option
Copy the code code as follows:
function removeAll(){
var obj=document.getElementByIdx_x('mySelect');
obj.options.length=0;
}
4. Delete an option option
Copy the code code as follows:
function removeOne(){
var obj=document.getElementByIdx_x('mySelect');
//index, the serial number of the option to be deleted, here takes the serial number of the currently selected option
var index=obj.selectedIndex;
obj.options.remove(index);
}
5. Get the value of option option
var obj=document.getElementByIdx_x('mySelect');
var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option
var val = obj.options[index].value;
6. Get the text of option option
var obj=document.getElementByIdx_x('mySelect');
var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option
var val = obj.options[index].text;
7. Modify option option
var obj=document.getElementByIdx_x('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");
8.Delete select
Copy the code code as follows:
function removeSelect(){
var mySelect = document.getElementByIdx_x("mySelect");
mySelect.parentNode.removeChild(mySelect);
}
The complete code for the entire example is as follows:
Copy the code code as follows:
<!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.getElementByIdx_x(id)
}
function show()
{
var selectObj=$("area")
var myOption=document.createElement_x("option")
myOption.setAttribute("value","10")
myOption.appendChild(document.createTextNode("Shanghai"))
var myOption1=document.createElement_x("option")
myOption1.setAttribute("value","100")
myOption1.appendChild(document.createTextNode("Nanjing"))
selectObj.appendChild(myOption)
selectObj.appendChild(myOption1)
}
functionchoice()
{
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_x("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_x("select")
nj.add(new Option("Xuanwu District","201"))
nj.add(new Option("白下区","202"))
nj.add(new Option("Xiaguan District","203"))
nj.add(new Option("Qixia District","204"))
$("context").appendChild(nj)
}
}
functioncalc()
{
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" onchange="choice()">
</select>
</div>
<input type=button value="Show" onclick="show()">
<input type=button value="Calculation node" onclick="calc()">
<input type=button value="Delete" onclick="remove()">
</body>
</html>
Improved version: add, modify, and delete option elements in select
Copy the code code as follows:
function watch_ini(){ // initial
for(var i=0; i<arguments.length; i++){
var word = document.createElement_x("OPTION");
word.text = arguments[i];
watch.keywords.add(word); // watch. is form name
}
}
function watch_add(f){ // add
var word = document.createElement_x("OPTION");
word.text = f.word.value;
f.keywords.add(word);
}
However, the above add() method is only valid under IE. In order to be compatible with FF and Opera, the above code has been improved. The modified code is as follows:
Copy the code code as follows:
function watch_ini(){ // initial
for(var i=0; i<arguments.length; i++){
var oOption=new Option(arguments[i],arguments[i]);
document.getElementByIdx_x("MySelect")[i]=oOption;
}
}
function watch_add(f){ // add
var oOption=new Option(f.word.value,f.word.value);
f.keywords[f.keywords.length]=oOption;
}
The complete code for the entire example is as follows:
Copy the code code as follows:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>javascript select options text value</title>
<meta name="keywords" content="javascript select options text value add modify delete set">
<meta name="description" content="javascript select options text value add modify delete set">
<script language="javascript">
<!--
function watch_ini(){ // initial
for(var i=0; i<arguments.length; i++){
var oOption=new Option(arguments[i],arguments[i]);
document.getElementByIdx_x("MySelect")[i]=oOption;
}
}
function watch_add(f){ // add
var oOption=new Option(f.word.value,f.word.value);
f.keywords[f.keywords.length]=oOption;
}
function watch_sel(f){ // Edit
f.word.value = f.keywords[f.keywords.selectedIndex].text;
}
function watch_mod(f){ // Modify
f.keywords[f.keywords.selectedIndex].text = f.word.value;
}
function watch_del(f){ // Delete
f.keywords.remove(f.keywords.selectedIndex);
}
function watch_set(f){ // save
var set = "";
for(var i=0; i<f.keywords.length; i++){
set += f.keywords[i].text + ";";
}
confirm(set);
}
//-->
</script>
</head>
<body>
<form name="watch" method="post" action="">
<select id="MySelect" name="keywords" size="10" onchange="watch_sel(this.form)"></select><br>
<script language="javascript">
<!--
watch_ini("I","You","","He","She","It","Er"); // Initial keyword
//-->
</script>
<input type="text" name="word" /><br />
<input type="button" value="Add" onclick="watch_add(this.form);" />
<input type="button" value="Modify" onclick="watch_mod(this.form);" />
<input type="button" value="Delete" onclick="watch_del(this.form);" />
<input type="button" value="Save" onclick="watch_set(this.form);" />
</form>
Create an array method from a string:
Copy the code code as follows:
<script language="javascript">
</body>
</html>
<script>
function spli(){
datastr="2,2,3,5,6,6";
var str= new Array();
str=datastr.split(",");
for (i=0;i<str.length ;i++)
{ document.write(str[i]+"<br/>"); }
}
spli();
</script>
I debugged a piece of js today and it ran fine on IE. However, an error occurred when debugging on Firefox. I checked the information and found that
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
Because of this code, I searched for information on the Internet and collected this article to review the old js in real time.