取得下拉列表中所选的选项的索引位置

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function alertIndex()
  5. {
  6. var x=document.getElementById("mySelect").selectedIndex;
  7. var y=document.getElementsByTagName("option");
  8. alert(y[x].text + " has the index of: " + y[x].index);
  9. }
  10. </script>
  11. </head>
  12. <body>
  13.  
  14. <form>
  15. 请选择您喜欢的水果:
  16. <select id="mySelect">
  17. <option>苹果</option>
  18. <option>桃子</option>
  19. <option>香蕉</option>
  20. <option>桔子</option>
  21. </select>
  22. <br />
  23. <br />
  24. <input type="button" onclick="alertIndex()" value="显示被选水果的 index">
  25. </form>
  26.  
  27. </body>
  28. </html>