在下拉列表中显示所选选项的索引

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. function getIndex() {
  6. document.getElementById("demo").innerHTML =
  7. document.getElementById("mySelect").selectedIndex;
  8. }
  9. </script>
  10. </head>
  11. <body>
  12.  
  13. <form>
  14. 请选择您喜欢的水果:
  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="显示所选选项的索引">
  24. </form>
  25.  
  26. <p id="demo"></p>
  27.  
  28. </body>
  29. </html>