1. Check whether Value & quot; paraValue & quot; exists in the select option; item2 adds an Item3 item to the select option to delete an Item4 item from the select option to delete option 5 selected in the select option modify the text of value & quot; paraValue & quot; In the select option to & quot; paraText & qu
1. check whether there is an Item with Value = "paraValue" in the select option.
2. add an Item to the select option
3. delete an Item from the select option
4. Delete the selected items in the select statement.
5. In the select option, set the text of value = "paraValue" to "paraText"
6. Set the first Item of text = "paraText" in select as the selected Item.
7. Set Item of value = "paraValue" in select to selected
8. Obtain the value of the selected item in the select statement.
9.
10. Obtain the Index of the selected item in the select statement.
11 Clear select items
Js Code
// 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 item www.2cto.com
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 of the select statement.
Var currSelectValue = document. all. objSelect. value;
// 9. Obtain 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;
Excerpt from advancing with the times