http://postimg.org/image/5u8xrw3vh/
我希望当我选择图书时,下面的类别如产品[菜单,名称,发布商等]只会在我选择图书时显示,而我不希望电影,音乐等相同的类别。请建议我。
答案 0 :(得分:0)
您有两个下拉列表,并且您希望根据用户在第一个下拉列表中选择的内容填充第二个下拉列表:
<select id="category_dropdown">
<option value="Movies">Movies</option>
<option value="Books">Books</option>
</select>
<select id='product_dropdown'></select>
要执行此操作,请使用以下JavaScript:
var product_menu = [];
//Books
product_menu["Books"] = [
"Author",
"Title",
"Publisher"
];
//Movies
product_menu["Movies"] = [
"Actor",
"Producer",
"Movie Title"
];
//Setup listener on the category drop-down to update the products menu
var category_dropdown = document.getElementById("category_dropdown");
category_dropdown.addEventListener("change", function(){
var product_dropdown = document.getElementById("product_dropdown");
//Remove all options from product menu
product_dropdown.innerHTML = "";
//Now add new options
var product_items = product_menu[category_dropdown.value];
for(var i = 0; i < product_items.length; i++){
var option = document.createElement('option');
option.text = product_items[i];
option.value = product_items[i];
product_dropdown.add(option);
}
});
如果您觉得有用,请标记为答案,谢谢!