源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编程字典</title> </head> <body> <p>从下面列表选一辆新车.</p> <select id="mySelect" onchange="myFunction()"> <option value="奥迪">奥迪 <option value="宝马">宝马 <option value="奔驰">奔驰 <option value="沃尔沃">沃尔沃 </select> <p>当你选择一辆新车时,一个函数被触发,输出选择的值</p> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("mySelect").value; document.getElementById("demo").innerHTML = "你选择的是: " + x; } </script> </body> </html>
运行结果