Show/hide fields depending on select value

后端 未结 6 864
执念已碎
执念已碎 2020-11-30 04:38

I am trying to show and hide a few form fields depending on the value of one of my select fields. I am looking to use arrays to hold what should be shown and what should not

6条回答
  •  执笔经年
    2020-11-30 05:09

    To fire up the code on load, just add .change(). As shown below...

    $(document).ready(function() {
      $.viewMap = {
        '0' : $([]),
        'view1' : $('#view1'),
        'view2' : $('#view2a, #view2b'),
        'view3' : $('#view3')
      };
    
      $('#viewSelector').change(function() {
        // hide all
        $.each($.viewMap, function() { this.hide(); });
        // show current
        $.viewMap[$(this).val()].show();
      }).change();
    });
    

提交回复
热议问题