JS select operations

Source: Internet
Author: User

 

1. dynamically create select

Function createselect (){

VaR myselect = Document. createelement ("select ");
Myselect. ID = "myselect ";
Document. Body. appendchild (myselect );
}

2. Add Option

Function addoption (){

// Search for objects by 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"); // compatible with IE and Firefox
}

3. Delete All option options

Function removeall (){
VaR OBJ = Document. getelementbyid ('myselect ');
OBJ. Options. Length = 0;

}

4. delete an option Option

Function removeone (){
VaR OBJ = Document. getelementbyid ('myselect ');

// Index. The sequence number of the option to be deleted. The sequence number of the selected option is used here.

VaR Index = obj. selectedindex;
OBJ. Options. Remove (INDEX );

 

In IE
Selectid. Options. Remove (selectid. selectedindex );
And
Selectid. Remove (selectid. selectedindex); can be used,
However, I don't know the former in Firefox, but I don't know the latter.

}

5. Obtain the option value.

VaR OBJ = Document. getelementbyid ('myselect ');

VaR Index = obj. selectedindex; // the serial number of the selected option.

Var val = obj. Options [Index]. value;

6. Obtain the option text

VaR OBJ = Document. getelementbyid ('myselect ');

VaR Index = obj. selectedindex; // the serial number of the selected option.

Var val = obj. Options [Index]. text;

7. Modify Option

VaR OBJ = Document. getelementbyid ('myselect ');

VaR Index = obj. selectedindex; // the serial number of the selected option.

Var val = obj. Options [Index] = New Option ("new text", "New Value ");

8. Delete select

Function removeselect (){
VaR myselect = Document. getelementbyid ("myselect ");
Myselect. parentnode. removechild (myselect );
}

 

Integrity of the entire instanceCodeAs 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. 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 ("region", "201 "))
NJ. Add (New Option ("White Zone", "202 "))
NJ. Add (New Option ("xianguang 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 = "show" onclick = "show ()">
<Input type = button value = "computing node" onclick = "calc ()">
<Input type = button value = "delete" onclick = "remove ()">
</Body>
</Html>

 

Optional version: add, modify, and delete option elements in the SELECT statement.

Function watch_ini () {// initial
For (VAR I = 0; I <arguments. length; I ++ ){
VaR word = Document. createelement ("option ");
Word. Text = arguments [I];
Watch. Keywords. Add (Word); // watch. Is form name
}
}
Function watch_add (f) {// Add
VaR word = Document. createelement ("option ");
Word. Text = f. Word. value;
F. Keywords. Add (Word );
}
However, the above add () method is only valid in IE. To be compatible with ff and opera, the above Code is improved. The changed code is as follows: function watch_ini () {// initial
For (VAR I = 0; I <arguments. length; I ++ ){
VaR ooption = New Option (arguments [I], arguments [I]);
Document. getelementbyid ("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 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 () {// initial
For (VAR I = 0; I <arguments. length; I ++ ){
VaR ooption = New Option (arguments [I], arguments [I]);
Document. getelementbyid ("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", "you", "he", "she", "it", "er"); // initial keyword
// -->
</SCRIPT>
<Input type = "text" name = "word"/> <br/>
<Input type = "button" value = "added" 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>

Use a string to create an array:

<Script language ="JavaScript">

  1. </Body>
    </Html>
  2. <SCRIPT>
  3. FunctionSpli (){
  4. Datastr ="2, 2, 3, 5, 6";
  5. VaRSTR =NewArray ();
  6. STR = datastr. Split (",");
  7. For(I = 0; I <Str. length; I ++)
  8. {Document. Write (STR [I] +"<Br/>");}
  9. }
  10. Spli ();
  11. </SCRIPT>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.