Js operation select and option of examples

  • 2020-03-30 01:00:08
  • OfStack

1. Create a select dynamically


     function createSelect(){
var mySelect = document.createElement_x("select");
          mySelect.id = "mySelect"; 
          document.body.appendChild(mySelect);
      }

2. Add options

     function addOption(){
          //Looking up objects by id,
           var obj=document.getElementByIdx_x('mySelect');
           //Add an option
obj.add(new Option(" The text "," value "));    //This only works in IE
         obj.options.add(new Option("text","value")); //This is compatible with IE and firefox
     }

3. Delete all options

     function removeAll(){
           var obj=document.getElementByIdx_x('mySelect');
obj.options.length=0;
     }

4. Remove an option

function removeOne(){
           var obj=document.getElementByIdx_x('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 the 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 the 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]. The text;

7. Modify options

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. To delete the select


function removeSelect(){
var mySelect = document.getElementByIdx_x("mySelect");
mySelect.parentNode.removeChild(mySelect);
     }

The complete code of the entire instance is 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)
    }
    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_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 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" onchange="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>

Improved version: add, modify, and delete option elements in select

function watch_ini(){ //  The 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){ //  increase 
var word = document.createElement_x("OPTION");
word.text = f.word.value;
f.keywords.add(word); 
}

However, the above method of add() is only valid in IE. In order to be compatible with FF and Opera, the above code has been improved as follows:

function watch_ini(){ //  The 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){ //  increase 
   var oOption=new Option(f.word.value,f.word.value);
   f.keywords[f.keywords.length]=oOption;
}

The complete code of the entire instance is 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(){ //  The 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){ //  increase 
   var oOption=new Option(f.word.value,f.word.value);
   f.keywords[f.keywords.length]=oOption;
}
function watch_sel(f){ //  The editor 
f.word.value = f.keywords[f.keywords.selectedIndex].text;
}
function watch_mod(f){ //  Modify the 
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 "," , "); //Initial keyword
//-->
</script>
<input type="text" name="word" /><br />
<input type="button" value=" increase " onclick="watch_add(this.form);" />
<input type="button" value=" Modify the " 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 with a string:

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

Today, I debugged a section of js, running well in ie, debugging errors in firefox, check the data found

Obj. add(new Option(" text "," value "));       // this only works in IE
                Obj. Options. The add (new Option (" text ", "value")); // this is compatible with IE and firefox

Because of this code, in the Internet to look up the data collection of this article, real-time wenjian js


Related articles: