1
|
1.html代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<
html
>
<
head
>
</
head
>
<
body
>
<
form
name="form1" id="form1">
Select your favorite fruit:
<
select
id="mySelect" name="mySelect">
<
option
value="11">Apple</
option
>
<
option
value="22" >Orange</
option
>
<
option
value="33">Pineapple</
option
>
<
option
value="44">Banana</
option
>
</
select
>
<
br
/><
br
/>
<
input
type="button" onclick="getIndex()"
value="Alert index of selected option">
</
form
>
</
body
>
</
html
>
|
2.javascript代码如下:
1
2
3
4
5
6
7
8
9
10
|
<script type=
"text/javascript"
>
function
getIndex()
{
//从document对象中,获取select标签
var
a=document.getElementById(
"mySelect"
);
//select标签获取的值其实是一个数组--a.options[]; 然后,选定项的下标是--a.selectedIndex
var
b=a.options[a.selectedIndex].value;
alert(b);
}
</script>
|
本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/3617920.html,如需转载请自行联系原作者