简体   繁体   English

根据其他下拉列表的选择填充一个下拉列表

[英]Populate one dropdown list based on the selection of other dropdown list

I want to create two drop down lists, category and item. 我想创建两个下拉列表,类别和项目。

If I select one of the categories named car, then item drop down list should have Honda, Volvo, Nissan. 如果我选择一个名为car的类别,那么项目下拉列表应该包含Honda,Volvo,Nissan。

If I select one of the categories named phone, then item drop down list should have this iPhone, Samsung, Nokia. 如果我选择一个名为phone的类别,那么项目下拉列表应该有这个iPhone,三星,诺基亚。

How can I do this? 我怎样才能做到这一点? I know that I can't do it with plain HTML. 我知道我不能用纯HTML做到这一点。

WORKING DEMO http://jsfiddle.net/kasperfish/r7MN9/3/ (with jquery) 工作演示http://jsfiddle.net/kasperfish/r7MN9/3/ (与jquery)

cars=new Array("Mercedes","Volvo","BMW","porche");
phones=new Array('Samsung','Nokia','Iphone');

populateSelect();

$(function() {

      $('#cat').change(function(){
        populateSelect();
    });

});


function populateSelect(){
    cat=$('#cat').val();
    $('#item').html('');


    if(cat=='car'){
        cars.forEach(function(t) { 
            $('#item').append('<option>'+t+'</option>');
        });
    }

    if(cat=='phone'){
        phones.forEach(function(t) {
            $('#item').append('<option>'+t+'</option>');
        });
    }

} 

UPDATED : using eval() to be able to add as much arrays as you want. 更新 :使用eval()可以添加任意数量的数组。 JSFIDDLE DEMO JSFIDDLE DEMO

cars=new Array("Mercedes","Volvo","BMW","porche");
phones=new Array('Samsung','Nokia','Iphone');
names=new Array('Kasper','Elke','Fred','Bobby','Frits');
colors=new Array('blue','green','yellow');

populateSelect();

$(function() {

      $('#cat').change(function(){
        populateSelect();
    });

});


function populateSelect(){
    cat=$('#cat').val();
    $('#item').html('');

       eval(cat).forEach(function(t) { 
            $('#item').append('<option>'+t+'</option>');
        });
    }

There are numerous ways that you can accomplish this depending on what your end goal is. 根据您的最终目标,您可以通过多种方式实现此目标。 Here are the 2 most common ones. 这是最常见的2个。

This is the basic process: 这是基本过程:

  1. Page loads with the initial dropdown populated with options, let's say Category (Car, Truck, SUV) and the item drop-down disabled. 使用选项填充初始下拉列表的页面加载,假设类别(汽车,卡车,SUV)和项目下拉列表已禁用。
  2. User selects a value from the first drop-down, there are a few ways to handle this: 用户从第一个下拉列表中选择一个值,有几种方法可以处理:

AJAX (the most seamless experience with fewest page loads): AJAX(最少页面加载的无缝体验):

  1. Using Javascript send an ajax request to a PHP script (or any other handler) containing the value of the selected option from the category drop-down as either a post or request parameter. 使用Javascript将包含所选选项值的PHP脚本(或任何其他处理程序)的ajax请求作为post或request参数发送到类别下拉列表中。
  2. Retrieve the values for the item drop-down based on our category, these could be from our database, a variable, a static file, or any other means that you can come up with 根据我们的类别检索项目下拉列表的值,这些值可以来自我们的数据库,变量,静态文件或您可以提出的任何其他方法
  3. Return our values as a response to the AJAX request (json, xml, plaintext, whatever fits best for you and you're most comfortable using) 返回我们的值作为对AJAX请求的响应(json,xml,plaintext,最适合你的任何东西,你最舒服的使用)
  4. Using Javascript (or you could use a library like jQuery) we insert the returned options from our AJAX request into the item drop-down and enable it so the user can make a selection. 使用Javascript(或者您可以使用类似jQuery的库),我们将AJAX请求中返回的选项插入到项目下拉列表中并启用它,以便用户可以进行选择。
  5. From here we can either continue using AJAX requests and responses or POST the form a return a final page, or whatever your particular usage calls for. 从这里我们可以继续使用AJAX请求和响应,或者POST表单返回最后一页,或者您的特定用法要求。

If you don't want to use AJAX, you could very easily POST the form to a Server-side handler, get the value from the category drop-down, locate your values for the item drop-down and then render your HTML response in which you set a value for the category drop-down and disable it (so the user would have to use the back button if they would wanted to change the category) and populate the item drop-down. 如果您不想使用AJAX,您可以非常轻松地将表单POST到服务器端处理程序,从类别下拉列表中获取值,找到项目下拉列表的值,然后呈现HTML响应您为类别下拉列表设置了一个值并将其禁用(因此如果用户想要更改类别,则必须使用后退按钮)并填充项目下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM