"Go" JS in the basic operation of Select

Source: Internet
Author: User

Determine if the item value= "Paravalue" exists in the Select option

//1. Determine if the value= "Paravalue" item exists in the SELECT optionfunctionJsselectisexititem (Objselect, objitemvalue) {varIsexit =false;  for(vari = 0; i < objSelect.options.length; i++) {                if(Objselect.options[i].value = =objitemvalue) {Isexit=true;  Break; }            }            returnIsexit; }      

Add an item to the Select option

//2. Add an item to the Select optionfunctionJsadditemtoselect (Objselect, Objitemtext, objitemvalue) {//determine if there is    if(Jsselectisexititem (Objselect, Objitemvalue)) {alert ("The value of the item already exists"); } Else {                varVaritem =NewOption (Objitemtext, Objitemvalue);             ObjSelect.options.add (Varitem); Alert ("Join successfully"); }        }       

Remove an item from the Select option

//3. Remove an item from the Select optionfunctionJsremoveitemfromselect (Objselect, objitemvalue) {//determine if there is    if(Jsselectisexititem (Objselect, Objitemvalue)) { for(vari = 0; i < objSelect.options.length; i++) {                    if(Objselect.options[i].value = =objitemvalue)                        {ObjSelect.options.remove (i);  Break; }} alert ("Successfully deleted"); } Else{alert ("The item does not exist in this select"); }        }    

Delete selected items in select

// 4. Delete the selected    item in select function Jsremoveselecteditemfromselect (objselect) {            var length = Objselect.options.length-1;         for (var i = length; I >= 0; i--) {            iftrue) {                 NULL ;            }        }    }      

Modify the Select option in Value = " Paravalue " the text is " Paratext "  

//5. Modify the text of value= "Paravalue" in the Select option to "Paratext"functionJsupdateitemtoselect (Objselect, Objitemtext, objitemvalue) {//determine if there is    if(Jsselectisexititem (Objselect, Objitemvalue)) { for(vari = 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 this select"); }        }        

set text in select = " Paratext " The first item is selected

//6. Set the first item of text= "Paratext" in Select to be selectedfunctionJsselectitembyvalue (Objselect, objitemtext) {//determine if there is    varIsexit =false;  for(vari = 0; i < objSelect.options.length; i++) {                if(Objselect.options[i].text = =Objitemtext) {objselect.options[i].selected=true; Isexit=true;  Break; }            }                  //Show out Results    if(isexit) {alert ("Selected successfully"); } Else{alert ("The item does not exist in this select"); }        }        

set value in select = " Paravalue " the item is selected

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

gets the value of the currently selected item of select

// 8. Gets the value    of the current selected item of select var currselectvalue = Document.all.objSelect.value;    

Gets the text of the current selected item of select

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

Gets the index of the current selected item of select

// 10. Get the index    of the current selected item of select var currselectindex = Document.all.objSelect.selectedIndex;    

Empty the Select item

// 11. Clear the Select item     document.all.objSelect.options.length = 0;  

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.