显示下拉列表中的所有选项

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. function getOptions() {
  6. var x = document.getElementById("mySelect");
  7. var txt = "";
  8. var i;
  9. for (i = 0; i < x.length; i++) {
  10. txt = txt + " " + x.options[i].text;
  11. }
  12. document.getElementById("demo").innerHTML = txt;
  13. }
  14. </script>
  15. </head>
  16. <body>
  17.  
  18. <form>
  19. 请选择您喜欢的水果:
  20. <select id="mySelect">
  21. <option>Apple</option>
  22. <option>Orange</option>
  23. <option>Pineapple</option>
  24. <option>Banana</option>
  25. </select>
  26. <br><br>
  27. <input type="button" onclick="getOptions()" value="输出所有选项">
  28. </form>
  29.  
  30. <p id="demo"></p>
  31.  
  32. </body>
  33. </html>
  34.