Jquery dependent drop down boxes populate- how

后端 未结 3 471
滥情空心
滥情空心 2020-11-29 12:55

I\'ve got dependent drop down boxes as shown on scenario below. Could anyone please suggest how to achieve the result using JQuery/Javascript?

Scenario:



        
3条回答
  •  自闭症患者
    2020-11-29 13:07

    The short answer is Yes it is possible in JavaScript/Jquery.

    You could do it like this defining your dependencies in a brand new HTML5 data-* attribute :-)

    HTML like this:

    HH1:
    
    
    HH2:
    
    

    And Javascript(JQuery) like this:

    $('select[name="drop1"]').change(function () {
    var ar = $('select[name="drop1"] option:selected').attr('data-shown').split(',');
    $('select[name="drop2"] option').each(function(){
        $(this).toggle(false);
        for(var i = 0; i <= ar.length; i++){
            if($(this).attr('value') == ar[i]){
            $(this).toggle(true);
            }
        }
    });}).trigger('change');
    

    That should do what you want done on your drop downs.

    With the condition you put in the comment it is a bit easier:

    $('select[name="drop1"]').change(function () {
        var number = parseInt($('option:selected', this).attr('value'));
        $('select[name="drop2"] option').each(function(){
                $(this).toggle(parseInt($(this).attr('value')) >= number);
            });
    }).trigger('change');
    

提交回复
热议问题