Home > Web Front-end > JS Tutorial > How to implement a drop-down list to select a value in js (3 methods)_javascript skills

How to implement a drop-down list to select a value in js (3 methods)_javascript skills

WBOY
Release: 2016-05-16 15:25:10
Original
2130 people have browsed it

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>

Copy after login

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>
.......
Copy after login

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

document.all( "mycombo ").selectedIndex=2 // 成功选中“苹果”这一项
mycombo.selectedIndex=2 // 成功选中“苹果”这一项 
document.mycombo.selectedIndex=2 // 失败 

Copy after login

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 // 成功选中“买水果”这一项

Copy after login

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();">

Copy after login

The JS function code is as follows:

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

Copy after login

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

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