SoFunction
Updated on 2025-02-28

JavaScript's operation example explanation of drop-down list box (select)


<script type="text/javascript">
<!--
var f = ("f");

//Get the number of select list items
();
();

//The subscript of the currently selected item (starting from 0) (there are two methods)
//If multiple items are selected, return the subscript of the first selected item
();
();

//Check whether an item is selected
([0].selected);

//Get the value and text of a certain item
([0].value);
([1].text);

//Delete a certain item
[1] = null;

//Add one
[] = new Option("appended text", "appended value");

//Change one
[1] = new Option("changed text", "changed value");
//You can also set the text and value of this item directly
//-->
</script>


//Select all items in the list
function SelectAllOption(list)
{
for (var i=0; i<; i++)
{
[i].selected = true;
}
}


//Unselect items in the list by asp learning network
function DeSelectOptions(list)
{
for (var i=0; i<; i++)
{
[i].selected = ![i].selected;
}
}


//Return to the number of selected items in the list
function GetSelectedOptionsCnt(list)
{
var cnt = 0;
var i = 0;
for (i=0; i<; i++)
{
if ([i].selected)
{
cnt++;
}
}

return cnt;
}


//Clear the list
function ClearList(list)
{
while ( > 0)
{
[0] = null;
}
}


//Delete the list selected item
//Return the number of deleted items
function DelSelectedOptions(list)
{
var i = 0;
var deletedCnt = 0;
while (i < )
{
if ([i].selected)
{
[i] = null;
deletedCnt++;
}
else
{
i++;
}
}

return deletedCnt;
}
//This function finds whether the corresponding item exists
// Whether to perform repeatCheck check
//If it is "v", repeat the value check by value
//If it is "t", repeat the value check according to the text
//If it is "vt", repeat the value check by value and text
//Other values, no repetitive check is performed, return false
function OptionExists(list, optText, optValue, repeatCheck)
{
var i = 0;
var find = false;

if (repeatCheck == "v")
{
//Repeat value check by value
for (i=0; i<; i++)
{
if ([i].value == optValue)
{
find = true;
break;
}
}
}
else if (repeatCheck == "t")
{
//Repeat the check by text
for (i=0; i<; i++)
{
if ([i].text == optText)
{
find = true;
break;
}
}
}
else if (repeatCheck == "vt")
{
//Repeat check by value and text
for (i=0; i<; i++)
{
if (([i].value == optValue) && ([i].text == optText))
{
find = true;
break;
}
}
}

return find;
}


//Add an item to the list
//list is the list to be added
//optText and optValue represent the text and values ​​of the item respectively
//RepeatCheck Whether to perform repetitive checks, see OptionExists
//Return true after successful addition, false after failure
function AppendOption(list, optText, optValue, repeatCheck)
{
if (!OptionExists(list, optText, optValue, repeatCheck))
{
[] = new Option(optText, optValue);
return true;
}
else
{
return false;
}
}


//Insert item
//index Insert position. When the insertion position >= list number of existing items, its function is equivalent to additional items that do not perform repeated inspections.
//optText and optValue represent the text and values ​​of the item respectively
function InsertOption(list, index, optText, optValue)
{
var i = 0;
for (i=; i>index; i--)
{
[i] = new Option([i-1].text, [i-1].value);
}

[index] = new Option(optText, optValue);
}
//Import items from one list to another list
// Whether to perform repeatCheck checks, see OptionExists
//After importing the deleteSource item to the target, whether to delete the item in the source list
//Return the number of items affected
function ListToList(sList, dList, repeatCheck, deleteSource)
{
//The number of rows affected
var lines = 0;
var i = 0;
while (i<)
{
if ([i].selected && AppendOption(dList, [i].text, [i].value, repeatCheck))
{
//Added successfully
lines++;
if (deleteSource)
{
//Delete items in the source list
[i] = null;
}
else
{
i++;
}
}
else
{
i++;
}
}

return lines;
}


//Move the selected item in the list up
function MoveSelectedOptionsUp(list)
{
var i = 0;
var value = "";
var text = "";
for (i=0; i<(-1); i++)
{
if (![i].selected && [i+1].selected)
{
value = [i].value;
text = [i].text;
[i] = new Option([i+1].text, [i+1].value);
[i].selected = true;
[i+1] = new Option(text, value);
}
}
}


//The selected item in the list moves down
function MoveSelectedOptionsDown(list)
{
var i = 0;
var value = "";
var text = "";
for (i=-1; i>0; i--)
{

//
if (![i].selected && [i-1].selected)
{
value = [i].value;
text = [i].text;
[i] = new Option([i-1].text, [i-1].value);
[i].selected = true;
[i-1] = new Option(text, value);
}
}
}