Common Code sorting for select and option operations in js

Source: Internet
Author: User

1. Obtain the selected select value and text. The html code is as follows::
Copy codeThe Code is as follows:
<Select id = "mySelect">
<Option value = "1"> one </option>
<Option value = "2"> two </option>
<Option value = "3"> three </option>
</Select>

You can use the following script code s to obtain the selected value and text
Copy codeThe Code is as follows:
$ ("# MySelect"). val (); // obtain the value of the selected record
$ ("# MySelect option: selected"). text (); // obtain the text value of the selected record

2. Use the new Option ("text", "value") method to add option
Copy codeThe Code is as follows:
Var obj = document. getElementById ("mySelect"); obj. add (new Option ("4", "4 "));

3. Delete All option options
Copy codeThe Code is as follows:
Var obj = document. getElementById ("mySelect"); obj. options. length = 0;

4. delete option
Copy codeThe Code is as follows:
Var obj = document. getElementById ("mySelect"); var index = obj. selectedIndex; obj. options. remove (index );

5. Modify the option
Copy codeThe Code is as follows:
Var obj = document. getElementById ("mySelect ");
Var index = obj. selectedIndex;
Obj. options [index] = new Option ("three", 3); // change the corresponding value
Obj. options [index]. selected = true; // keep selected

6. Delete select
Copy codeThe Code is as follows:
Var obj = document. getElementById ("mySelect ");
Obj. parentNode. removeChild (obj); // remove the current object

7. select selected RESPONSE event
Copy codeThe Code is as follows:
$ ("# MySelect"). change (function () {// Add the operation code to be executed })

1. dynamically create select
Copy codeThe Code is as follows:
Function createSelect (){
Var mySelect = document. createElement_x ("select ");
MySelect. id = "mySelect ";
Document. body. appendChild (mySelect );
}

2. Add option
Copy codeThe Code is as follows:
Function addOption (){
// Search for objects by id,
Var obj = document. getElementByIdx_x ('myselect ');
// Add an option
Obj. add (new Option ("text", "value"); // This can only be valid in IE
Obj. options. add (new Option ("text", "value"); // compatible with IE and firefox
}

3. Delete All option options
Copy codeThe Code is as follows:
Function removeAll (){
Var obj = document. getElementByIdx_x ('myselect ');
Obj. options. length = 0;
}

4. delete an option
Copy codeThe Code is as follows:
Function removeOne (){
Var obj = document. getElementByIdx_x ('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 );
}

5. Obtain the option value.
Copy codeThe Code is as follows:
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index]. value;

6. Obtain the option text
Copy codeThe Code is as follows:
Var obj = document. getElementByIdx_x ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index]. text;

7. Modify option
Copy codeThe Code is as follows:
Var obj = document. getElementByIdx_x ('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
Copy codeThe Code is as follows:
Function removeSelect (){
Var mySelect = document. getElementByIdx_x ("mySelect ");
MySelect. parentNode. removeChild (mySelect );
}

The complete code for the entire instance is as follows::
Copy codeThe Code 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 ("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.
Copy codeThe Code is 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 add () method is only valid in IE. To be compatible with FF and Opera, the above Code is improved. The code after modification is as follows:
Copy codeThe 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. 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 instance is as follows::
Copy codeThe Code 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. 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", "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:
Copy codeThe Code is as follows:
<Script language = "javascript">
</Body>
</Html>
<Script>
Function spli (){
Datastr = "2, 2, 3, 5, 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 code today and it runs well on ie. I encountered an error in debugging on Firefox.
Copy codeThe Code is as follows:
Obj. add (new Option ("text", "value"); // This can only be valid in IE
Obj. options. add (new Option ("text", "value"); // compatible with IE and firefox

Because of this code, I collected this article on the Internet to search for information.
Sharing!

Related Article

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.