JavaScript operations Select Method Encyclopedia [Add, modify, delete, select, empty, Judge presence etc]_ form effects

Source: Internet
Author: User
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 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 from 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. To 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 for select
var currselectvalue = Document.all.objSelect.value;

//9. Get the text of the current selected item for 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;
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.