layui是一个用户比较多的前端开发框架之一。其优点就是开发便捷、界面美观等。这篇笔记主要实现的是根据下拉选框中选择不同的值,出现不同的DIV(显示不同的内容)。这个描述应该大家能够理解哈。其大致示例图如下:
HTML部分源码:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <meta charset="utf-8">
- <title>layui</title>
- <meta name="renderer" content="webkit">
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
- <link rel="stylesheet" href="css/layui.css" media="all">
- <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 -->
- <style type="text/css">
- #formInform_name{
- width:60%;
- float: left;
- }
- </style>
- </head>
- <body>
- <div class="">
- <div id="formInform_name" class="layui-form-item">
- <label class="layui-form-label">企业</label>
- <div class="layui-input-block">
- <input id="search_info" type="text" name="title" required="" lay-verify="required" placeholder="搜索" autocomplete="off" class="layui-input">
- </div>
- </div>
- <div>
- <div id="search_btn" class="layui-btn layui-btn-normal">搜索</div>
- </div>
- <form class="layui-form" >
- <div class="layui-form-item">
- <label class="layui-form-label">企业名称</label>
- <div class="layui-input-block">
- <input id="change_info" type="text" name="title" lay-verify="title" autocomplete="off" placeholder="企业名称" class="layui-input">
- </div>
- <div style="display:none;">
- <label class="layui-form-label">企业ID</label>
- <div class="layui-input-block">
- <input hidden="hidden" type="text" name="title" lay-verify="title" autocomplete="off" placeholder="企业名称" class="layui-input">
- </div>
- </div>
- </div>
- <div id="selected" class="layui-form-item">
- <label class="layui-form-label">置顶类型</label>
- <div class="layui-input-inline">
- <select lay-filter="test" >
- <option value="" selected="">请选择</option>
- <option class="layui-btn" value="0">展商列表</option>
- <option class="layui-btn" value="1">区域置顶</option>
- <option class="layui-btn" value="2">行业置顶</option>
- </select>
- </div>
- </div>
- <div id="show_add" class="layui-form-item" style="display:none;">
- <label class="layui-form-label">区域地区</label>
- <div class="layui-input-inline">
- <select name="quiz1">
- <option value="1" selected="">请选择省</option>
- <option value="浙江">浙江省</option>
- <option value="你的工号">江西省</option>
- <option value="你最喜欢的老师">福建省</option>
- </select>
- </div>
- <div class="layui-input-inline" >
- <select name="quiz2">
- <option value="1">请选择市</option>
- <option value="杭州">杭州</option>
- <option value="宁波">宁波</option>
- <option value="温州">温州</option>
- <option value="温州">台州</option>
- <option value="温州">绍兴</option>
- </select>
- </div>
- <div class="layui-input-inline lay">
- <select name="quiz3">
- <option value="">区域地区</option>
- <option value="西湖区">西湖区</option>
- <option value="余杭区">余杭区</option>
- <option value="拱墅区">临安市</option>
- </select>
- </div>
- </div>
- <div id="show_HY" class="layui-form-item" style="display: none;">
- <label class="layui-form-label">选择行业</label>
- <div class="layui-input-inline">
- <select name="quiz1">
- <option value="" selected="">一级</option>
- <option value="浙江">浙江省</option>
- <option value="你的工号">江西省</option>
- <option value="你最喜欢的老师">福建省</option>
- </select>
- </div>
- <div class="layui-input-inline">
- <select name="quiz2">
- <option value="">二级</option>
- <option value="杭州">杭州</option>
- <option value="宁波" disabled="">宁波</option>
- <option value="温州">温州</option>
- <option value="温州">台州</option>
- <option value="温州">绍兴</option>
- </select>
- </div>
- </div>
- <div class="layui-form-item" style="padding:10px 0 ;">
- <div class="layui-input-block">
- <button class="layui-btn" >保存</button>
- <button type="reset" class="layui-btn layui-btn-primary">取消</button>
- </div>
- </div>
- </form>
- <script src="layui.js" charset="utf-8"></script>
- <!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 -->
- <script>
- layui.use(['form','layer','jquery'], function(){
- var $ = layui.jquery,
- layer = layui.layer,
- form = layui.form;
- form.on('select(test)', function(data){
- console.log(data)
- console.log(data.elem); //得到select原始DOM对象
- console.log(data.value); //得到被选中的值
- console.log(data.othis); //得到美化后的DOM对象
- var value = data.value;
- if(value == "" || value == "0"){
- $("#show_add").hide();
- $("#show_HY").hide();
- }else if(value == "1"){
- $("#show_add").show();
- $("#show_HY").hide();
- }else{
- $("#show_add").hide();
- $("#show_HY").show();
- }
- })
- })
- </script>
- </body>
- </html>
关于完整源码可点击此处下载: