源代码 :
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>开源教程(ossoft.cn)</title> <script> function getOptions(){ var x=document.getElementById("mySelect"); var txt="选中的选项是: "; var i; for (i=0;i<x.length;i++){ if(x.options[i].selected) { txt=txt + "\n" + x.options[i].text; } } alert(txt); } </script> </head> <body> <form> 你喜欢的水果是: <select id="mySelect"> <option>苹果</option> <option>橙子</option> <option>菠萝</option> <option>香蕉</option> </select> <br><br> <input type="button" onclick="getOptions()" value="输出选中选项"> </form> </body> </html>
运行结果