如何利用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" 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>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM