layui+formSelects实现下拉多选菜单几十种示例代码
代码语言:html
所属分类:表单美化
代码描述:layui+formSelects实现下拉多选菜单几十种示例代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>BFW NEW PAGE</title> <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/formSelects-v4.css"> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/layui/layui.js"></script> <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/js/layui/css/layui.css"> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/formSelects-v4.min.js"></script> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/highlight.js"></script> </head> <body> <blockquote class="layui-elem-quote layui-quote-nm" id="examples"> 示例 </blockquote> <div class="example"> <h3 id="select_default" title=" 1) 基础多选">基础多选</h3> <p> 适用性较广的基础多选,用 Tag 展示已选项 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select1"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_pane" title=" 2) pane模式">pane模式</h3> <p> layui pane模式 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form layui-form-pane"> <div class="layui-form-item"> <label class="layui-form-label">多选框</label> <div class="layui-input-block"> <select name="city" xm-select="select2"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected" disabled="disabled">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> </div> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_group" title=" 3) 分组多选">分组多选</h3> <p> 基于layui的分组多选 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select3"> <optgroup label="分组1"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> </optgroup> <optgroup label="分组2"> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </optgroup> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_tips" title=" 4) 自定义tips">自定义tips</h3> <p> 写一个空的option放在第一个 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select4"> <option value="">啦啦, 我是自定义的</option> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_max" title=" 5) 多选上限">多选上限</h3> <p> xm-select-max="3", 超过3个后, 默认闪烁红框提示 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select5" xm-select-max="3"> <option value="">啦啦, 我是自定义的</option> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 当然, 如果你初始化值就多于max值, 那是可以设置成功滴, 取消了就选不回来了 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select5_2" xm-select-max="3"> <option value="">啦啦, 我是自定义的</option> <option value="1" disabled="disabled" selected="selected">北京</option> <option value="2" selected="selected">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_skin" title=" 6) 多选皮肤">多选皮肤</h3> <p> xm-select-skin="default" </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select6_1" xm-select-skin="default"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> xm-select-skin="primary", 这个墨绿style已经被内置成为了经典皮肤, 只因他诞生于layui的经典颜色 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select6_2" xm-select-skin="primary"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> xm-select-skin="normal" </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select6_3" xm-select-skin="normal"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> xm-select-skin="warm" </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select6_4" xm-select-skin="warm"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> xm-select-skin="danger" </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select6_5" xm-select-skin="danger"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_search_local" title=" 7) 本地搜索">本地搜索</h3> <p> xm-select-search, 搜索已存在的option </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select7_1" xm-select-search> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 分组中的搜索 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select7_2" xm-select-search> <optgroup label="分组1"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> </optgroup> <optgroup label="分组2"> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </optgroup> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_search_server" title=" 8) 远程搜索">远程搜索</h3> <p> xm-select-search="/search", 指定一个url, 将开启远程搜索模式 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select8" xm-select-search="https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> <p> 远程搜索response结构, 其中name, value可以自定义, 请查看<a href="#methods-config">method config</a> </p> <p> code为0时, 表示正常, 不为0时则提示msg信息 </p> <pre class="brush: js"> { "code": 0, "msg": "success", "data": [ {"name":"北京","value":1,"selected":"","disabled":""}, {"name":"上海","value":2,"selected":"","disabled":""}, {"name":"广州","value":3,"selected":"selected","disabled":""}, {"name":"深圳","value":4,"selected":"","disabled":"disabled"}, {"name":"天津","value":5,"selected":"","disabled":""} ] } </pre> </div> <p> 当然, 远程搜索也是可以分组的 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select8_2" xm-select-search="https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/group"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> <pre class="brush: js"> { "code": 0, "msg": "success", "data": [ {"name": "分组-1", "type": "optgroup"}, {"name":"北京","value":1,"selected":"","disabled":""}, {"name":"上海","value":2,"selected":"","disabled":""}, {"name": "分组-2", "type": "optgroup"}, {"name":"广州","value":3,"selected":"selected","disabled":""}, {"name":"深圳","value":4,"selected":"","disabled":"disabled"}, {"name":"天津","value":5,"selected":"","disabled":""} ] } </pre> </div> </div> <div class="example"> <h3 id="select_create" title=" 9) 创建条目">创建条目</h3> <p> xm-select-create, 不存在时会创建, 选中后长久保留, value为Date.now()生成 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select9" xm-select-search xm-select-create> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_direction" title=" 10) 下拉方向">下拉方向</h3> <p> 方式一: xm-select-direction="auto|up|down", 自动, 上, 下, 默认自动模式 </p> <p> 方式二: 使用formSelects.config配置 </p> <pre class="brush: js"> //配置某一个多选的方向 layui.formSelects.config('select1', {direction: 'up'}); //配置所有多选的方向 layui.formSelects.config({direction: 'up'}); </pre> <p> 我是朝上的 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select10_1" xm-select-direction="up"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 我是朝下的 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select10_2" xm-select-direction="down"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 其他的都是自动处理的哈, 当然没有朝左朝右的 </p> <p> 我是一个动态的 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <select name="city" xm-select="select10_3" xm-select-direction="down"> <option value="1" disabled="disabled">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5">天津</option> </select> </div> <button class="layui-btn layui-btn-normal example-btn" onclick="formSelects.config('select10_3', {direction: 'up'});">点我朝上</button> <button class="layui-btn layui-btn-normal example-btn" onclick="formSelects.config('select10_3', {direction: 'down'});">点我朝下</button> </div> </div> <div class="example"> <h3 id="select_height" title=" 11) 固定高度">固定高度</h3> <p> xm-select-height="36px", 多选的高度是随便选项的变化而变化的, 当然你也可以固定一个高度, 从此高度将不再变化 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select11" xm-select-height="36px"> <option value="1" selected="selected">北京</option> <option value="2" selected="selected">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_disabled" title=" 12) 禁用select">禁用select</h3> <p> disabled="disabled", 多选的高度是随便选项的变化而变化的, 当然你也可以固定一个高度, 从此高度将不再变化 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select12" disabled="disabled"> <option value="1">北京</option> <option value="2">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 动态操作select的启用与禁用 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select12_2" disabled="disabled"> <option value="1">北京</option> <option value="2">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> <button class="layui-btn layui-btn-normal example-btn" onclick="formSelects.undisabled('select12_2')">点我启用</button> <button class="layui-btn layui-btn-normal example-btn" onclick="formSelects.disabled('select12_2')">点我禁用</button> <button class="layui-btn layui-btn-normal example-btn" onclick="formSelects.undisabled()">全部启用</button> <button class="layui-btn layui-btn-normal example-btn" onclick="formSelects.disabled()">全部禁用</button> </div> </div> <div class="example"> <h3 id="select_radio" title=" 13) 单选模式">单选模式</h3> <p> xm-select-radio, 单选模式 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select13" xm-select-radio> <option value="1">北京</option> <option value="2">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_form" title=" 14) 表单操作">表单操作</h3> <p> 支持layui原生表单验证, lay-verify="xxx" </p> <div class="layui-form"> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select14" lay-verify="select14"> <option value="">选择3项以上通过验证</option> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> <option value="6">杭州</option> </select> </div> </div> <button class="layui-btn example-btn" lay-submit lay-filter="select14">验证提交</button> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> tips模式, lay-verify="required", lay-verType="tips" </p> <div class="layui-form"> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select14_1" lay-verify="required" lay-verType="tips"> <option value="">选择3项以上通过验证</option> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> <option value="6">杭州</option> </select> </div> </div> <button class="layui-btn example-btn" lay-submit lay-filter="select14_1">验证提交</button> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 支持表单提交自动赋值, 只需要在select加上name属性即可, 点击提交可查看url的变化 </p> <div> <form class="layui-form" action=""> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select14_2"> <option value="1" selected="selected">北京</option> <option value="2" selected="selected">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> <option value="6">杭州</option> </select> </div> </div> <button class="layui-btn layui-btn-normal example-btn" type="submit">提交表单</button> <button class="layui-btn layui-btn-normal example-show-code" type="button"></button> </form> </div> <p> 支持reset操作, 选择几项数据, 然后点击重置 </p> <div> <form class="layui-form"> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select14_3"> <option value="">默认提示</option> <option value="1" selected="selected">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> <option value="6">杭州</option> </select> </div> </div> <button class="layui-btn layui-btn-normal example-btn" type="reset">重置表单</button> <button class="layui-btn layui-btn-normal example-show-code" type="button"></button> </form> </div> </div> <div class="example"> <h3 id="select_linkage" title=" 15) 多选联动">多选联动</h3> <p> 多选联动, 联动需要使用data来赋值, 先给一个空select标记多选, <b>记得value不能重复哦</b> </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select15"> <option value="">请选择, 此处是联动多选</option> </select> <js> layui.formSelects.data('select15', 'local', { arr: [ { "name": "北京", "value": 1, "children": [ {"name": "北京市1", "value": 12, "children": [ {"name": "朝阳区1", "value": 13, "children": []}, {"name": "朝阳区2", "value": 14, "children": []}, {"name": "朝阳区3", "value": 15, "children": []}, {"name": "朝阳区4", "value": 16, "children": []}, ]}, {"name": "北京市2", "value": 17, "children": []}, {"name": "北京市3", "value": 18, "children": []}, {"name": "北京市4", "value": 19, "children": []}, ] }, { "name": "天津", "value": 2, "children": [ {"name": "天津市1", "value": 51, "children": []}, ] }, ], linkage: true }); //如果有需要默认值的小伙伴请使用formSelects.value formSelects.value('select15', ['1/12/13', '1/12/14']) </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 多选联动, 使用远程数据实现省市区三级联动 </p> <p> 宽度是可以自定义的, linkageWidth: 130, 默认100 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select15_2"> <option value="">请选择, 此处是联动多选</option> </select> <js> formSelects.config('select15_2', { success: function(id, url, val, result){ console.log("success回调: " + url); }, error: function(id, url, val, err){ console.log("err回调: " + url); } }); formSelects.data('select15', 'server', { url: 'http://yapi.demo.qunar.com/mock/9813/layui/citys', linkage: true, linkageWidth: 130 }); //如果有需要默认值的小伙伴请使用formSelects.value formSelects.value('select15', ['1/12/13', '1/12/14']) </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_btns" title=" 16) 快捷操作">快捷操作</h3> <p> 设置内置的快捷操作 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select16_1"> <option value="1">北京</option> <option value="2">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> <js> formSelects.btns('select16_1', ['select', 'remove', 'reverse', 'skin']); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 自定义快捷操作 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select16_2"> <option value="1">北京</option> <option value="2">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> <js> formSelects.btns('select16_2', ['select', 'remove', 'skin', { icon: 'layui-icon layui-icon-ok', //自定义图标, 可以使用layui内置图标 name: '提示名称', click: function(id){ //点击后的操作 alert('点击了自定义快捷操作') } }]); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 你是可以换数组位置, 更换显示位置滴 </p> <p> 如果内置操作不满意, 完全可以自定义 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select16_3"> <option value="1">北京</option> <option value="2">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> <js> formSelects.btns('select16_3', ['skin', { icon: 'layui-icon layui-icon-ok', //自定义图标, 可以使用layui内置图标 name: '提示名称', click: function(id){ //点击后的操作 alert('点击了自定义快捷操作') } }, 'remove']); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 如果不需要快捷操作, 设置一个空数组就可以了, 它会变回以前的模样 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select16_4"> <option value="1">北京</option> <option value="2">上海</option> <option value="3" selected="selected">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> <option value="6" selected="selected">杭州</option> </select> <js> formSelects.btns('select16_4', []); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 只显示图标 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select16_5"> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> <option value="6">杭州</option> </select> <js> formSelects.btns('select16_5', ['select', 'remove'], {show: 'icon'}); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 只显示名称 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select16_6"> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> <option value="6">杭州</option> </select> <js> formSelects.btns('select16_6', ['select', 'remove'], {show: 'name'}); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 显示图标+名称, 但是是紧凑型的 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="select16_7"> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> <option value="6">杭州</option> </select> <js> formSelects.btns('select16_7', ['select', 'remove'], {show: '', space: '10px'}); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <blockquote class="layui-elem-quote layui-quote-nm" id="examples2"> 7、进阶示例 </blockquote> <div class="example"> <h3 id="select_on" title=" 1) 监听select选择">监听select选择</h3> <p> 适用于动态操作 </p> <p> 选择[北京]时, 会发现选不上哦 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example1"> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> </select> <js> layui.formSelects.on('example1', function(id, vals, val, isAdd, isDisabled){ //id: 点击select的id //vals: 当前select已选中的值 //val: 当前select点击的值 //isAdd: 当前操作选中or取消 //isDisabled: 当前选项是否是disabled alert("选择了: " + val.name); //如果return false, 那么将取消本次操作 if(val.value == 1){ return false; } }); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 获取实时数据 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example1_1"> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> </select> <js> layui.formSelects.on('example1_1', function(id, vals, val, isAdd, isDisabled){ //id: 点击select的id //vals: 当前select已选中的值 //val: 当前select点击的值 //isAdd: 当前操作选中or取消 //isDisabled: 当前选项是否是disabled alert("当前选择了: " + JSON.stringify(vals)); }, true); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_value" title=" 2) 赋值与取值">赋值与取值</h3> <p> 多选取值 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example2_1"> <option value="1" selected="selected">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> </select> <js> layui.formSelects.value('example2_1'); //取值默认数组 layui.formSelects.value('example2_1', 'val'); //取值val数组 layui.formSelects.value('example2_1', 'valStr'); //取值val字符串 layui.formSelects.value('example2_1', 'name'); //取值name数组 layui.formSelects.value('example2_1', 'nameStr'); //取值name字符串 </js> </div> </div> <button class="layui-btn example-btn" onclick="alert(JSON.stringify(formSelects.value('example2_1')))">默认数组</button> <button class="layui-btn example-btn" onclick="alert(JSON.stringify(formSelects.value('example2_1', 'val')))">val数组</button> <button class="layui-btn example-btn" onclick="alert(JSON.stringify(formSelects.value('example2_1', 'valStr')))">val字符串</button> <button class="layui-btn example-btn" onclick="alert(JSON.stringify(formSelects.value('example2_1', 'name')))">name数组</button> <button class="layui-btn example-btn" onclick="alert(JSON.stringify(formSelects.value('example2_1', 'nameStr')))">name字符串</button> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 多选赋值 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example2_2"> <option value="1" selected="selected">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> </select> <js> layui.formSelects.value('example2_2', []); //赋值空数组, 清空所有 layui.formSelects.value('example2_2', [1, 2]); //赋值 北京,上海 layui.formSelects.value('example2_2', [5], true); //追加赋值 天津 layui.formSelects.value('example2_2', [1], false); //删除 已选择 [北京] </js> </div> </div> <button class="layui-btn example-btn" onclick="formSelects.value('example2_2', [])">清空所有</button> <button class="layui-btn example-btn" onclick="formSelects.value('example2_2', [1, 2])">赋值 北京,上海</button> <button class="layui-btn example-btn" onclick="formSelects.value('example2_2', [5], true)">追加赋值 天津</button> <button class="layui-btn example-btn" onclick="formSelects.value('example2_2', [1], false)">删除 已选择 [北京]</button> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_maxtips" title=" 3) 超选后的提示">超选后的提示</h3> <p> 如果设置了xm-select-max="3", 当你选择第四个的时候会默认闪烁红框提示, 当然这个提示你可以自定义 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example3" xm-select-max="2"> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4" selected="selected">深圳</option> <option value="5" selected="selected">天津</option> </select> <js> layui.formSelects.maxTips('example3', function(id, vals, val, max){ //id: 点击select的id //vals: 当前select已选中的值 //val: 当前select点击的值 //max: 当天多选最大值 alert("选超了, 选择的值: " + val.value); }); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_filter" title=" 4) 自定义搜索">自定义搜索</h3> <p> 如果设置了xm-select-search="", 当你搜索的时候是默认判断选项文本是否包含搜索的值 </p> <p> 如下是检测 全拼,简拼,文本是否包含, 其中汉字转拼音摘录于网络 </p> <p> 可以尝试使用拼音进行搜索 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example4" xm-select-search=""> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广州</option> <option value="4">深圳</option> <option value="5">天津</option> </select> <js> layui.formSelects.filter('example4', function(id, inputVal, val, isDisabled){ if( PY.fullPY(val.name).toLowerCase().indexOf(inputVal) != -1 || //拼音全拼是否包含 PY.fullPY(val.name, true).indexOf(inputVal) != -1 || //拼音简拼是否包含 val.name.indexOf(inputVal) != -1 //文本是否包含 ){ return false; } return true; }); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 友情提示: 远程搜索是不会经过此方法滴 </p> </div> <div class="example"> <h3 id="select_config" title=" 5) 配置">配置</h3> <p> 说起来这是个又爱又恨的方法, 设计之初是准备专门配置远程搜索用的, 无奈后面增加了很多东西, 结果变成了专门的配置方法了 </p> <p> 好像没啥好说的, 都是点儿配置的东西, 说点儿场景吧 </p> <p> - 默认本地数据, js动态配置成远程搜索, 首先开启搜索模式 xm-select-search="", 然后config中定义url, 适用于url为可变参数的情况下 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example5_1" xm-select-search=""> <option value="">哈哈, 默认是没有的, 显示的是远程数据</option> </select> <js> layui.formSelects.config('example5_1', { searchUrl: 'https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data' }); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> - 点击加载 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example5_2" xm-select-search=""> <option value="">哈哈, 默认是没有的</option> </select> <js> layui.formSelects.config('example5_2', { searchUrl: 'https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data' }); </js> </div> </div> <button class="layui-btn example-btn" onclick="formSelects.config('example5_2', {searchUrl: 'https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data'})">点我加载</button> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> - beforeSuccess的用法 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example5_3" xm-select-search=""> <option value="">哈哈, 默认是没有的</option> </select> <js> layui.formSelects.config('example5_3', { searchUrl: 'https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data', beforeSuccess: function(id, url, searchVal, result){ //我要把数据外层的code, msg, data去掉 result = result.data; //我要反转name $.each(result, function(index, item) { item.name && (item.name = item.name.split('').reverse().join('')) }); //然后返回数据 return result; } }); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 当然上面的处理方法是用于演示beforeSuccess的用法的, 正确的姿势应该是转换数据结构滴 </p> <p> success, error可以直接打开控制台, 查看console的打印记录 </p> <p> <b>远程数据的赋值</b> </p> <p> 远程数据是使用ajax异步的模式来获取的, 所以赋值不能直接在config之后 </p> <p> 方式一: 在返回数据中包含selected属性 </p> <p> 方式二: 在success回调中使用value进行赋值 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example5_4" xm-select-search=""> <option value="">哈哈, 我没赋值成功</option> </select> <js> layui.formSelects.config('example5_4', { searchUrl: 'https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data', success: function(id, url, searchVal, result){ formSelects.value('example5_4', [3, 4]); } }); //赋值失败了, 我的天, 什么情况, 结果发现对应3,4的value根本不存在!!! 请往下看 </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> <p> 上面好像不太对 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example5_5" xm-select-search=""> <option value="">哈哈, 默认是没有的</option> </select> <js> layui.formSelects.config('example5_5', { searchUrl: 'https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data', success: function(id, url, searchVal, result){ formSelects.value('example5_5', [result.data[0].value, result.data[4].value]); } }); </js> </div> </div> <button class="layui-btn layui-btn-normal example-show-code"></button> </div> </div> <div class="example"> <h3 id="select_data" title=" 6) 动态数据">动态数据</h3> <p> 本地数据 </p> <div> <div style="width: 400px; display: inline-block; vertical-align: top;"> <div class="layui-form"> <select name="city" xm-select="example6_1"> <option value="">来自星星的你</option> </select> <js> layui.formSelec.........完整代码请登录后点击上方下载按钮下载查看
网友评论0