Home >Web Front-end >JS Tutorial >How to implement a drop-down list to select a value in js (3 methods)_javascript skills
The example in this article describes the method of selecting a certain value in a drop-down list using js. Share it with everyone for your reference, the details are as follows:
Method 1:
<select id="aa"> <option>1</option> <option>2</option> </select> <input type="button" value=" 选中" onclick="checkOption()" /> <script language="javascript" type="text/javascript" > function checkOption() { document.getElementById("aa").options[1].selected = true; alert("选中了2"); } </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
document.all( "mycombo ").selectedIndex=2 // 成功选中“苹果”这一项 mycombo.selectedIndex=2 // 成功选中“苹果”这一项 document.mycombo.selectedIndex=2 // 失败
Operation of drop-down list box combo2 within the form myform
document.all( "combo2 ").selectedIndex=2 // 成功选中“买水果”这一项 myform.combo2.selectedIndex=2 // 成功选中“买水果”这一项 document.myform.combo2.selectedIndex=2 // 成功选中“买水果”这一项
Method 3:
Can be assigned by value.
For example:
The drop-down list code is as follows:
<select onPropertyChange="showValue(this.value)" id="mysel"> <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() { document.getElementById("mysel").value="2"; }
I hope this article will be helpful to everyone in JavaScript programming.