1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 如何利用javascript获取表单中select下拉列表中所选中项的值value

如何利用javascript获取表单中select下拉列表中所选中项的值value

时间:2018-12-12 02:31:29

相关推荐

如何利用javascript获取表单中select下拉列表中所选中项的值value

1.html代码如下:

<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" οnclick="getIndex()"value="Alert index of selected option"></form></body></html>

2.javascript代码如下:

<script type="text/javascript">function getIndex(){//从document对象中,获取select标签var a=document.getElementById("mySelect");//select标签获取的值其实是一个数组--a.options[]; 然后,选定项的下标是--a.selectedIndexvar b=a.options[a.selectedIndex].value; alert(b); } </script>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。