jQuery: Conditional show an element based on drop down box selection

后端 未结 2 1646
孤城傲影
孤城傲影 2020-12-10 09:35

I have two related drop-down lists, in which the contents in the second drop-down list depends on the selection made in the first one. For example, in the following HTML cod

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 10:15

    Your questions describe the right ideas. You just have to structure your HTML to take advantage of them.

    JSFiddle posted here: http://jsfiddle.net/iknowkungfoo/TKamw/

    HTML - I added an ID and CLASS to each TR that match the values in your primary SELECT:

    CSS - hide those TRs by default:
    tr.method_options { display: none; }​

    JavaScript/jQuery - When the primary SELECT changes, hide all TRs with a CLASS of "method_options". Then, Find the TR whose ID matches the value of the selected option in the primary SELECT and show it. If there is no match, then nothing is shown.

    $(document).ready(function(){
    
        $('#id_application_method').on('change', function() {         
    
            $('tr.method_options').hide();
            $('#tr_' + $(this).val() ).show();
    
        });
    
    });

提交回复
热议问题