JavaScript Operations Select Reference Code _javascript Tips

Source: Internet
Author: User
1. Determine if there is a value= "Paravalue" Item in the Select option
function Jsselectisexititem (Objselect, Objitemvalue) {
var isexit = false;
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
Isexit = true;
Break
}
}
return isexit;
}

2. Add an item to the Select option
function Jsadditemtoselect (Objselect, Objitemtext, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
Alert ("The value of the item already exists");
} else {
var varitem = new Option (Objitemtext, Objitemvalue);
ObjSelect.options.add (Varitem);
Alert ("Join successfully");
}
}

3. Remove an item from the Select option
function Jsremoveitemfromselect (Objselect, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
ObjSelect.options.remove (i);
Break
}
}
Alert ("Successful deletion");
} else {
Alert ("The item does not exist in the select");
}
}


4. Delete selected items in select
function Jsremoveselecteditemfromselect (objselect) {
var length = objselect.options.length-1;
for (var i = length; I >= 0; i--) {
if (objselect[i].selected = = True) {
Objselect.options[i] = null;
}
}
}

5. Modify the Select option Value= "Paravalue" text is "Paratext"
function Jsupdateitemtoselect (Objselect, Objitemtext, Objitemvalue) {
To determine whether there is
if (Jsselectisexititem (Objselect, Objitemvalue)) {
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].value = = Objitemvalue) {
Objselect.options[i].text = Objitemtext;
Break
}
}
Alert ("Successful modification");
} else {
Alert ("The item does not exist in the select");
}
}

6. Set the first item of text= "Paratext" in Select to select
function Jsselectitembyvalue (Objselect, Objitemtext) {
To determine whether there is
var isexit = false;
for (var i = 0; i < objSelect.options.length; i++) {
if (Objselect.options[i].text = = Objitemtext) {
Objselect.options[i].selected = true;
Isexit = true;
Break
}
}
Show the results
if (isexit) {
Alert ("Successfully selected");
} else {
Alert ("The item does not exist in the select");
}
}

7. Set the item of value= "Paravalue" in Select to select
Document.all.objSelect.value = Objitemvalue;

8. Value of the current selected item of the Select
var currselectvalue = Document.all.objSelect.value;

9. Gets the text of the currently selected item of select
var currselecttext = Document.all.objselect.options[document.all.objselect.selectedindex].text;

10. Index of the current selected item for select
var currselectindex = Document.all.objSelect.selectedIndex;

11. Empty a Select item
document.all.objSelect.options.length = 0;

The above is collected from the Internet, now look at the contents of the above to do
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>javascript Select Combination _ Yun-Habitat community _www.jb51.net</title> <script language=" JavaScript "type=" Text/javascript > Function vtoption () {var S1=document.getelementbyid ("Select1"); var Sv=s1.options[s1.selectedindex].value; var St=s1.options[s1.selectedindex].text; Alert (sv+ "\ n" +st); function AddOption1 () {var S1=document.getelementbyid ("Select1"); var newop1=new Option; Newop1.value= "3"; Newop1.text= "This is 3"; var newop2=new Option; Newop2.value= "4"; Newop2.text= "This is 4"; S1.add (NEWOP1); S1.add (NEWOP2); function AddOption2 () {var S1=document.getelementbyid ("Select1"); for (Var i=3;i<5;i++) {var newop1=new Option ("This is" +i,i); var newop1=new Option; Newop1.value=i; Newop1.text= "This is"+i; S1.add (NEWOP1); } function AddOption3 () {var S1=document.getelementbyid ("Select1"); s1.options.length=0;//clears the previous option for (Var i=3;i<7;i++) {var newop1=new option ("This is" +i,i); var newop1=new Option; Newop1.value=i; Newop1.text= "This is" +i; S1.add (NEWOP1); } function Remoption () {var S1=document.getelementbyid ("Select1"); for (Var i=s1.options.length;i>=0;i--) {s1.options.remove (i); } function remoptionselected () {var S1=document.getelementbyid ("Select1"); var I=s1.selectedindex; alert (i); S1.options.remove (i); } </script> </pead> <body> <select name= "select" id= "Select1" > <option value= "1" > This is 1&L. T;/option> <option value= "2" > This is 2</option> </select> fetch value    increase 1   increase 2    Add 3   Delete options    Delete selection options </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.