JS Dynamic Change Select Select the index value of option change options _javascript Tips

Source: Internet
Author: User
document.getElementById ("Louyuming"). Options[0].selected=true;

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; 
}

The Javascript action Select is a common form of today, when you delete multiple select values, there is a problem, for half a day the original is caused by the index (that is, the deletion of the time to delete from the beginning of the index, and then delete the index small, otherwise deleted the index small index after the big index changes, There is a problem when you delete it--the key is for the for loop to be from big to small, not regular from 0 to length.

4. Delete selected item 
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; 
} 
} 
}

1 To determine if there is a value= "Paravalue" Item in the Select option
2 Add an item to the Select option
3 Delete an item from the Select option
4 Delete selected Items in select
5 Modify the Select option Value= "Paravalue" text is "Paratext"
6 Set the first item of text= "Paratext" in the Select to select
7 Set the item in the Select Value= "Paravalue" to select
8 Gets the value of the currently selected item of select
9 Gets the text of the currently selected item for select
10 gets the index of the current selected item of select
11 Empty A Select item

======================================================================

To delete all options in a select dynamically:

function Deletealloptions (SEL) { 
sel.options.length=0 
}

To delete an option in a select dynamically:

function Deleteoption (sel,indx) { 
sel.options.remove (indx); 
}

Dynamically add items in Select option:

function AddOption (sel,text,value) { 
sel.options.add (new Option (Text,value)); 
}

Above in IE and Firefox can be tested successfully, I hope that can be used later.

===========================================

JS Code

1. Determine if there is a value= "Paravalue" Item function Jsselectisexititem (Objselect, Objitemvalue) {var isexit = False in the Select option; 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) {//To determine if there is an if (jsselectis 
Exititem (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 function Jsremoveitemfromselect (Objselect, Objitemvalue) from the Select option {//To determine if there is an if (Jsselectisexiti TEM (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, Objite Mvalue) {//To determine if there is an 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 ("Successfully modified"); 
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 if Var IsE exists 
XIT = false; for (var i = 0; i < objSelect.options.length i++) {if (Objselect.options[i].text = = Objitemtext) {objselect.option 
S[i].selected = true; 
Isexit = true; 
Break 
//show results if (isexit) {alert ("successfully selected"); 
else {alert ("The item does not exist in the select"); }//7. Set value= in Select ParavAlue "The item is selected Objselect.value = Objitemvalue; 

8. value var currselectvalue = Objselect.value for the current selected item of select; 

9. The text var currselecttext = Objselect.options[document.all.objselect.selectedindex].text that gets the currently selected item of select; 

10. index var currselectindex = Objselect.selectedindex for the currently selected item of select; 11. Empty the Select item objSelect.options.length = 0;

The complete code for the entire instance is as follows:

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >  

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.