Show/Hide different forms based on a option selected

后端 未结 7 1337
暗喜
暗喜 2021-02-04 19:28

I would like to know how to show/hide different forms based one form\'s selection.

In the sample code below all three forms are automatically set to display:none. I woul

7条回答
  •  悲哀的现实
    2021-02-04 20:25

    $(document).ready(function() {
      $("select").change(function() {
        $(this).find("option:selected").each(function() {
          var optionValue = $(this).attr("value");
          if (optionValue) {
            $(".box").not("." + optionValue).hide();
            $("." + optionValue).show();
          } else {
            $(".box").hide();
          }
        });
      }).change();
    });
    .box {
      color: #fff;
      padding: 20px;
      display: none;
      margin-top: 20px;
    }
    
    .red {
      background: #ff0000;
    }
    
    .green {
      background: #228B22;
    }
    
    .blue {
      background: #0000ff;
    }
    
    
    
    
      
      jQuery Show Hide Elements Using Select Box
    
      
    
    
    
      
    You have selected red option so i am here
    You have selected green option so i am here
    You have selected blue option so i am here

提交回复
热议问题