layui根据select选项不同显示不同的DIV

layui是一个用户比较多的前端开发框架之一。其优点就是开发便捷、界面美观等。这篇笔记主要实现的是根据下拉选框中选择不同的值,出现不同的DIV(显示不同的内容)。这个描述应该大家能够理解哈。其大致示例图如下:

layui根据select选项显示div

HTML部分源码:

  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <meta charset="utf-8">
  6.         <title>layui</title>
  7.         <meta name="renderer" content="webkit">
  8.         <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  9.         <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  10.         <link rel="stylesheet" href="css/layui.css"  media="all">
  11.         <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 -->
  12.         <style type="text/css">
  13.             #formInform_name{
  14.                 width:60%;
  15.                 float: left;
  16.             }
  17.         </style>
  18.     </head>
  19.     <body>
  20.         <div  class="">
  21.             <div id="formInform_name" class="layui-form-item">
  22.                 <label class="layui-form-label">企业</label>
  23.                 <div  class="layui-input-block">
  24.                     <input id="search_info" type="text" name="title" required="" lay-verify="required" placeholder="搜索" autocomplete="off" class="layui-input">
  25.                 </div>
  26.             </div>
  27.             <div>
  28.                   <div id="search_btn" class="layui-btn layui-btn-normal">搜索</div>
  29.             </div>
  30.             <form class="layui-form" >
  31.             <div class="layui-form-item">
  32.                 <label class="layui-form-label">企业名称</label>
  33.                 <div class="layui-input-block">
  34.                   <input id="change_info" type="text" name="title" lay-verify="title" autocomplete="off" placeholder="企业名称" class="layui-input">
  35.                 </div>
  36.                 <div  style="display:none;">
  37.                     <label class="layui-form-label">企业ID</label>
  38.                     <div class="layui-input-block">
  39.                       <input hidden="hidden" type="text" name="title" lay-verify="title" autocomplete="off" placeholder="企业名称" class="layui-input">
  40.                     </div>
  41.                 </div>
  42.             </div>
  43.             <div id="selected" class="layui-form-item">
  44.                 <label class="layui-form-label">置顶类型</label>
  45.                 <div class="layui-input-inline">
  46.                   <select lay-filter="test" >
  47.                     <option value="" selected="">请选择</option>
  48.                     <option class="layui-btn" value="0">展商列表</option>
  49.                     <option class="layui-btn" value="1">区域置顶</option>
  50.                     <option class="layui-btn" value="2">行业置顶</option>
  51.                   </select>
  52.                 </div>
  53.             </div>
  54.             <div id="show_add" class="layui-form-item" style="display:none;">
  55.                 <label class="layui-form-label">区域地区</label>
  56.                 <div class="layui-input-inline">
  57.                   <select name="quiz1">
  58.                     <option value="1" selected="">请选择省</option>
  59.                     <option value="浙江">浙江省</option>
  60.                     <option value="你的工号">江西省</option>
  61.                     <option value="你最喜欢的老师">福建省</option>
  62.                   </select>
  63.                 </div>
  64.                 <div class="layui-input-inline" >
  65.                   <select name="quiz2">
  66.                     <option value="1">请选择市</option>
  67.                     <option value="杭州">杭州</option>
  68.                     <option value="宁波">宁波</option>
  69.                     <option value="温州">温州</option>
  70.                     <option value="温州">台州</option>
  71.                     <option value="温州">绍兴</option>
  72.                   </select>
  73.                 </div>
  74.                 <div class="layui-input-inline lay">
  75.                   <select name="quiz3">
  76.                     <option value="">区域地区</option>
  77.                     <option value="西湖区">西湖区</option>
  78.                     <option value="余杭区">余杭区</option>
  79.                     <option value="拱墅区">临安市</option>
  80.                   </select>
  81.                 </div>
  82.             </div>
  83.             <div id="show_HY" class="layui-form-item" style="display: none;">
  84.                 <label class="layui-form-label">选择行业</label>
  85.                 <div class="layui-input-inline">
  86.                   <select name="quiz1">
  87.                     <option value="" selected="">一级</option>
  88.                     <option value="浙江">浙江省</option>
  89.                     <option value="你的工号">江西省</option>
  90.                     <option value="你最喜欢的老师">福建省</option>
  91.                   </select>
  92.                 </div>
  93.                 <div class="layui-input-inline">
  94.                   <select name="quiz2">
  95.                     <option value="">二级</option>
  96.                     <option value="杭州">杭州</option>
  97.                     <option value="宁波" disabled="">宁波</option>
  98.                     <option value="温州">温州</option>
  99.                     <option value="温州">台州</option>
  100.                     <option value="温州">绍兴</option>
  101.                   </select>
  102.                 </div>
  103.             </div>
  104.             <div class="layui-form-item" style="padding:10px 0 ;">
  105.               <div class="layui-input-block">
  106.                 <button class="layui-btn" >保存</button>
  107.                 <button type="reset" class="layui-btn layui-btn-primary">取消</button>
  108.               </div>
  109.             </div>
  110.           </form>
  111.         <script src="layui.js" charset="utf-8"></script>
  112.         <!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 -->
  113.         <script>
  114.             layui.use(['form','layer','jquery'], function(){
  115.                 var $ = layui.jquery,
  116.                     layer = layui.layer,
  117.                     form = layui.form;
  118.                 form.on('select(test)', function(data){
  119.                     console.log(data)
  120.                     console.log(data.elem); //得到select原始DOM对象
  121.                     console.log(data.value); //得到被选中的值
  122.                     console.log(data.othis); //得到美化后的DOM对象
  123.                     var value = data.value;
  124.                     if(value == "" || value == "0"){
  125.                         $("#show_add").hide();
  126.                         $("#show_HY").hide();
  127.                     }else if(value == "1"){
  128.                         $("#show_add").show();
  129.                         $("#show_HY").hide();
  130.                     }else{
  131.                         $("#show_add").hide();
  132.                         $("#show_HY").show();
  133.                     }
  134.                 })
  135.             })
  136.         </script>
  137.     </body>
  138. </html>

关于完整源码可点击此处下载:

 

 

 

文件下载

社区直播小程序点击获取
你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!