JavaScript Select Add, modify, delete, select, empty, and judge existing functions

Source: Internet
Author: User

The code is as follows Copy Code

JS Code
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
function to the Select option Jsadditemtoselect (Objselect, Objitemtext, objitemvalue) {
    //Determine if there is a
    if (Jsselectisexititem (Objselect, Objitemvalue)) {
         alert ("The value of the item already exists");
   } else {
        var varitem = new Option (Objitemtext, obj Itemvalue);
        ObjSelect.options.add (varitem);
        alert ("Join successfully");
   }
}       

//3. Remove one item
function Jsremoveitemfromselect (Objselect, Objitemvalue) from the Select option {
   // To determine if there is a
    if (Jsselectisexititem (Objselect, Objitemvalue)) {
         for (var i = 0; I < objSelect.options.length i++) {
       &N bsp;    if (Objselect.options[i].value = = objitemvalue) {
                 ObjSelect.options.remove (i);
                break;
           }
       }
        Alert ("Successful deletion");
   } else {
        alert ("This item does not exist in this 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. Get the Value,index,text of the current selected item
function GetValue (obj) {
return obj.value;
}
9. Get the index of the currently selected item
function GetIndex (obj) {
return obj.selectedindex;
}
10. Get the text of the currently selected item
function GetText (obj) {
return obj.options[obj.selectedindex].text;
}
11. Clear all options
function Clear (obj) {
obj.options.length = 0;
}

1, to determine if there is a value= "Paravalue" Item
2 in the Select option, add an item
3 to the Select option, delete an item from the Select Option
4, and delete the selected item from the Select
5, modify the Select option Value= "Paravalue" text is "Paratext"
6, set the first item in the Select Text= "Paratext" to select
7, Set the item for value= "Paravalue" in Select to select
8, get the value of the current selected item for select
9, get the text
10 of the currently selected item for select, Gets the index
11 of the currently selected item for select, empties the Select item

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.