如何利用javascript获取表单中select下拉列表中所选中项的值value
1个回答
展开全部
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" onclick="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.selectedIndex
var b=a.options[a.selectedIndex].value;
alert(b);
}
</script>
<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代码如下:
<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>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询