1 Determine whether there is an Item with Value="paraValue" in the select option
2-to-select option add an Item
3 Delete an Item from the select option
4 Delete the selected item in select
5 Modify the text of value="paraValue" in the select option to "paraText"
6Set the first Item of text="paraText" in select as selected
7Set the Item of value="paraValue" in select as selected
8 Get the value of the currently selected item
9 Get the text of the currently selected item
10 Get the index of the currently selected item
11 Clear the selected item
js code
// 1. Determine whether there is an Item with Value="paraValue" in the select option
function jsSelectIsExitItem(objSelect, objItemValue) {
var isExit = false;
for (var i = 0; i < ; i++) {
if ([i].value == objItemValue) {
isExit = true;
break;
}
}
return isExit;
}
// 2. Add an Item to the select option
function jsAddItemToSelect(objSelect, objItemText, objItemValue) {
//Judge whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
alert("The Value value of this Item already exists");
} else {
var varItem = new Option(objItemText, objItemValue);
(varItem);
alert("joined successfully");
}
}
// 3. Remove an Item from the select option
function jsRemoveItemFromSelect(objSelect, objItemValue) {
//Judge whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < ; i++) {
if ([i].value == objItemValue) {
(i);
break;
}
}
alert("Successfully deleted");
} else {
alert("This item does not exist in this select");
}
}
// 4. Delete the selected item in select
function jsRemoveSelectedItemFromSelect(objSelect) {
var length = - 1;
for(var i = length; i >= 0; i--){
if(objSelect[i].selected == true){
[i] = null;
}
}
}
// 5. Modify the text of value="paraValue" in the select option to "paraText"
function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {
//Judge whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < ; i++) {
if ([i].value == objItemValue) {
[i].text = objItemText;
break;
}
}
alert("Successfully modified");
} else {
alert("This item does not exist in this select");
}
}
// 6. Set the first Item of text="paraText" in select as selected
function jsSelectItemByValue(objSelect, objItemText) {
//Judge whether it exists
var isExit = false;
for (var i = 0; i < ; i++) {
if ([i].text == objItemText) {
[i].selected = true;
isExit = true;
break;
}
}
//Show results
if (isExit) {
alert("Successfully selected");
} else {
alert("This item does not exist in this select");
}
}
// 7. Set the Item of value="paraValue" in select as selected
= objItemValue;
// 8. Get the value of the currently selected item
var currSelectValue = ;
// 9. Get the text of the currently selected item in select
var currSelectText = [].text;
// 10. Get the index of the currently selected item
var currSelectIndex = ;
// 11. Clear the selected item
= 0;
2-to-select option add an Item
3 Delete an Item from the select option
4 Delete the selected item in select
5 Modify the text of value="paraValue" in the select option to "paraText"
6Set the first Item of text="paraText" in select as selected
7Set the Item of value="paraValue" in select as selected
8 Get the value of the currently selected item
9 Get the text of the currently selected item
10 Get the index of the currently selected item
11 Clear the selected item
js code
// 1. Determine whether there is an Item with Value="paraValue" in the select option
function jsSelectIsExitItem(objSelect, objItemValue) {
var isExit = false;
for (var i = 0; i < ; i++) {
if ([i].value == objItemValue) {
isExit = true;
break;
}
}
return isExit;
}
// 2. Add an Item to the select option
function jsAddItemToSelect(objSelect, objItemText, objItemValue) {
//Judge whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
alert("The Value value of this Item already exists");
} else {
var varItem = new Option(objItemText, objItemValue);
(varItem);
alert("joined successfully");
}
}
// 3. Remove an Item from the select option
function jsRemoveItemFromSelect(objSelect, objItemValue) {
//Judge whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < ; i++) {
if ([i].value == objItemValue) {
(i);
break;
}
}
alert("Successfully deleted");
} else {
alert("This item does not exist in this select");
}
}
// 4. Delete the selected item in select
function jsRemoveSelectedItemFromSelect(objSelect) {
var length = - 1;
for(var i = length; i >= 0; i--){
if(objSelect[i].selected == true){
[i] = null;
}
}
}
// 5. Modify the text of value="paraValue" in the select option to "paraText"
function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {
//Judge whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < ; i++) {
if ([i].value == objItemValue) {
[i].text = objItemText;
break;
}
}
alert("Successfully modified");
} else {
alert("This item does not exist in this select");
}
}
// 6. Set the first Item of text="paraText" in select as selected
function jsSelectItemByValue(objSelect, objItemText) {
//Judge whether it exists
var isExit = false;
for (var i = 0; i < ; i++) {
if ([i].text == objItemText) {
[i].selected = true;
isExit = true;
break;
}
}
//Show results
if (isExit) {
alert("Successfully selected");
} else {
alert("This item does not exist in this select");
}
}
// 7. Set the Item of value="paraValue" in select as selected
= objItemValue;
// 8. Get the value of the currently selected item
var currSelectValue = ;
// 9. Get the text of the currently selected item in select
var currSelectText = [].text;
// 10. Get the index of the currently selected item
var currSelectIndex = ;
// 11. Clear the selected item
= 0;