donglian5309 2014-12-22 17:51
浏览 43

如何使用php数组和/或sql创建两个相关的html下拉列表

So I need to create two dropdown lists where second list is dependent on the first one. For example:

In first list I may have options like this:

  • A
  • B
  • C
  • D

When users selects A, then he will get to chose additional options in second dropdown based on the first dropdown. So In case user chose A in first list, options in second list may be:

  • A-1
  • A-2
  • A-3

How can I do this in php and html ? Do I have to use multidimensional arrays ? Maybe database ? How would I detect what user has selected in first list and offer him proper options in second ?

  • 写回答

2条回答 默认 最新

  • dosryjij88555 2014-12-22 20:40
    关注

    I did one the other day using a multidimensional array.

    var options = {
        "Option 1" : [ "1A","1B","1C" ],
        "Option 2" : [ "2A","2B","2C" ],
        "Option 3" : [ "3A","3B","3C" ]
    }
    

    Then something like:

    jQuery:

    $.each(options,function(a,b){
        $('#select1').append('<option>'+a+'</option>');
    });
    
    $('#select1').change(function(){
        var sel1 = $(this).val();
        $.each(option[$sel1],function(a,b) {
            $('#select2').append('<option>'+b+'</option>');
        });
    });
    

    (This is the concept... this actual syntax may or may not be error-ridden.)

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看