1. Determine whether the select option contains an item with value = "paravalue"
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 ){
// Determine whether the object exists
If (jsselectisexititem (objselect, objitemvalue )){
Alert ("the value of this item already exists ");
} Else {
VaR varitem = New Option (objitemtext, objitemvalue );
Objselect. Options. Add (varitem );
Alert ("successfully added ");
}
}
3. delete an item from the select option
Function jsremoveitemfromselect (objselect, objitemvalue ){
// Determine whether the object exists
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 ("deleted successfully ");
} Else {
Alert ("this select item does not exist ");
}
}
4. Delete the selected items in the SELECT statement.
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 text of value = "paravalue" in the select option to "paratext"
Function jsupdateitemtoselect (objselect, objitemtext, objitemvalue ){
// Determine whether the object exists
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 ("modified successfully ");
} Else {
Alert ("this select item does not exist ");
}
}
6. Set the first item of text = "paratext" in select to be selected.
Function jsselectitembyvalue (objselect, objitemtext ){
// Determine whether the object exists
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 result
If (isexit ){
Alert ("selected successfully ");
} Else {
Alert ("this select item does not exist ");
}
}
7. Set the item of value = "paravalue" in select to be selected.
Document. All. objselect. value = objitemvalue;
8. Obtain the value of the selected item in the SELECT statement.
VaR currselectvalue = Document. All. objselect. value;
9. Get the text of the selected item of the SELECT statement.
VaR currselecttext = Document. All. objselect. Options [document. All. objselect. selectedindex]. text;
10. Obtain the index of the selected item of the SELECT statement.
VaR currselectindex = Document. All. objselect. selectedindex;
11. Clear select items
Document. All. objselect. Options. Length = 0;
The above is collected from the Internet. Now let's take a look at what we have done based on the above content.Xmlns = "http://www.w3.org/1999/xhtml">
This is 1This is 2
Add 1, 2, 3, delete
Delete selected options