HTML DOM selectedIndex 属性

定义和用法

selectedIndex 属性可设置或返回下拉列表中被选选项的索引号。

注释:若允许多重选择,则仅会返回第一个被选选项的索引号。

语法

  1. selectObject.selectedIndex=number

实例

下面的例子可提示出被选选项的索引号:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function getIndex()
  5. {
  6. var x=document.getElementById("mySelect")
  7. alert(x.selectedIndex)
  8. }
  9. </script>
  10. </head>
  11. <body>
  12.  
  13. <form>
  14. Select your favorite fruit:
  15. <select id="mySelect">
  16. <option>Apple</option>
  17. <option>Orange</option>
  18. <option>Pineapple</option>
  19. <option>Banana</option>
  20. </select>
  21. <br /><br />
  22. <input type="button" onclick="getIndex()"
  23. value="Alert index of selected option">
  24. </form>
  25.  
  26. </body>
  27. </html>