Home > Web Front-end > JS Tutorial > body text

A complete list of Javascript select control operations (add, modify, delete, select, clear, determine existence, etc.)_Form special effects

WBOY
Release: 2016-05-16 18:57:32
Original
871 people have browsed it

1 Determine 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 item in the select
5 Modify the text of value="paraValue" in the select option to "paraText"
6 Set the first Item of text="paraText" in the select to be selected
7 Set the Item of value="paraValue" in the select to be selected
8 Get the value of the currently selected item of select
9 Get the text of the currently selected item of select
10 Get the Index of the currently selected item of select
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 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 it 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 it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i if (objSelect.options[i] .value == objItemValue) {
objSelect.options.remove(i);
break;
}
}
alert("Successfully deleted");
} else {
alert("This item does not exist in the select");
}
}
// 4. Delete the selected item in the 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 text of value="paraValue" in the select option to "paraText"
function jsUpdateItemToSelect(objSelect, objItemText , objItemValue) {
//Determine whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i if (objSelect.options[i].value == objItemValue) {
objSelect.options[i].text = objItemText;
break;
}
}
alert("Successfully modified" );
} else {
alert("This item does not exist in the select");
}
}
// 6. Set the first position of text="paraText" in the select An Item is selected
function jsSelectItemByValue(objSelect, objItemText) {
//Determine whether it exists
var isExit = false;
for (var i = 0; i if (objSelect.options[i].text == objItemText) {
objSelect.options[i].selected = true;
isExit = true;
break;
}
}
//Show the result
if (isExit) {
alert("Successfully selected");
} else {
alert("The item does not exist in the selection ");
}
}
// 7. Set the Item with value="paraValue" in the select to be selected
document.all.objSelect.value = objItemValue;
// 8. Get the value of the currently selected item of select
var currSelectValue = document.all.objSelect.value;
// 9. Get the text of the currently selected item of select
var currSelectText = document.all.objSelect.options [document.all.objSelect.selectedIndex].text;
// 10. Get the Index of the currently selected item of select
var currSelectIndex = document.all.objSelect.selectedIndex;
// 11. Clear the select Items of
document.all.objSelect.options.length = 0;

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!