JavaScript 参考手册
Select options 集合
通过另外一个下拉菜单已选选项来修改当前下拉菜单的选项。
源代码:
点击运行 »
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>蜜蜂教程(mifengjc.com)</title> <script> var carsAndModels = {}; carsAndModels['VO'] = ['V70', 'XC60', 'XC90']; carsAndModels['VW'] = ['Golf', 'Polo', 'Scirocco', 'Touareg']; carsAndModels['BMW'] = ['M6', 'X5', 'Z3']; function ChangeCarList() { var carList = document.getElementById("car"); var modelList = document.getElementById("carmodel"); var selCar = carList.options[carList.selectedIndex].value; while (modelList.options.length) { modelList.remove(0); } var cars = carsAndModels[selCar]; if (cars) { for (var i = 0; i < cars.length; i++) { var car = new Option(cars[i], i); modelList.options.add(car); } } } </script> </head> <body onload="ChangeCarList();"> <select id="car" onchange="ChangeCarList();"> <option value="">-- Car --</option> <option value="VO">Volvo</option> <option value="VW">Volkswagen</option> <option value="BMW">BMW</option> </select> <select id="carmodel"></select> </body> </html>
运行结果:
点击运行 »