SoFunction
Updated on 2025-02-28

js implements a method to select a certain value by drop-down list (3 methods)

This article describes the method of selecting a certain value by implementing the drop-down list by js. Share it for your reference, as follows:

Method 1:

<select >
<option>1</option>
<option>2</option>
</select>
<input type="button" value="Selected" onclick="checkOption()" />
<script language="javascript" type="text/javascript" >
function checkOption()
{
 ("aa").options[1].selected = true;
 alert("2 selected");
}
</script>

Method 2:

There is a drop down as follows

<select name="" >
<option value="1">11</option>
<option value="2">22</option>
<option value="3">33</option>
.......

Operation of mycombo drop-down list box not within the form

( "mycombo ").selectedIndex=2 // Successfully selected the "Apple" item=2 // Successfully selected the "Apple" item=2 // fail

Operation of drop-down list box combo2 in form myform

( "combo2 ").selectedIndex=2 // Successfully selected the item "Buy Fruit"myform.=2 // Successfully selected the item "Buy Fruit".=2 // Successfully selected the item "Buy Fruit"

Method 3:

Values ​​can be assigned.

As an example:

The drop-down list code is as follows:

<select onPropertyChange="showValue()" >
 <option value=""></option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
</select>
<input type="button" value="changevalue" onclick="setvalue();">

The JS function code is as follows:

function setvalue() {
 ("mysel").value="2";
}

I hope this article will be helpful to everyone's JavaScript programming.