layui根据单选框选项不同,切换表单内容。具体效果如下所示。
实现方式:
1、最顶部的radio单选按钮,必须赋予属性"lay-ignore",如果layui的form渲染后,会导致点击事件失效。
2、像通常一样编写表单代码。下面分享一部分片段。
- <div class="form-group" data-storage-type="year-week" lay-filter="year-week">
- <div class="layui-form-item">
- <label class="layui-form-label">每隔</label>
- <div class='layui-input-inline'>
- <input type="text" name="gap" required="required" value="{$vo.gap|default='1'}" class="layui-input">
- </div>
- <div class="layui-form-mid">年重复,</div>
- </div>
- <div class="layui-form-item">
- <label class="layui-form-label">在</label>
- <div class="layui-input-inline">
- <select name="month" style="width: 30%">
- <option value="0">请选择</option>
- <option value="1" {if isset($vo.month) and $vo.month eq 1}selected{/if}>一月</option>
- <option value="2" {if isset($vo.month) and $vo.month eq 1}selected{/if}>二月</option>
- <option value="3" {if isset($vo.month) and $vo.month eq 1}selected{/if}>三月</option>
- <option value="4" {if isset($vo.month) and $vo.month eq 1}selected{/if}>四月</option>
- <option value="5" {if isset($vo.month) and $vo.month eq 1}selected{/if}>五月</option>
- <option value="6" {if isset($vo.month) and $vo.month eq 1}selected{/if}>六月</option>
- <option value="7" {if isset($vo.month) and $vo.month eq 1}selected{/if}>七月</option>
- <option value="8" {if isset($vo.month) and $vo.month eq 1}selected{/if}>八月</option>
- <option value="9" {if isset($vo.month) and $vo.month eq 1}selected{/if}>九月</option>
- <option value="10" {if isset($vo.month) and $vo.month eq 1}selected{/if}>十月</option>
- <option value="11" {if isset($vo.month) and $vo.month eq 1}selected{/if}>十一月</option>
- <option value="12" {if isset($vo.month) and $vo.month eq 1}selected{/if}>十二月</option>
- </select>
- </div>
- <div class="layui-input-inline">
- <select name="week" class="layui-input-block" style="width: 30%">
- <option value="0">请选择</option>
- <option value="1" {if isset($vo.month) and $vo.month eq 1}selected{/if}>第一周</option>
- <option value="2" {if isset($vo.month) and $vo.month eq 1}selected{/if}>第二周</option>
- <option value="3" {if isset($vo.month) and $vo.month eq 1}selected{/if}>第三周</option>
- <option value="4" {if isset($vo.month) and $vo.month eq 1}selected{/if}>第四周</option>
- </select>
- </div>
- <div class="layui-input-inline">
- <select name="weekday" class="layui-input-block" style="width: 30%">
- <option value="0">请选择</option>
- <option value="monday" {if isset($vo.month) and $vo.month eq 1}selected{/if}>星期一</option>
- <option value="tuesday" {if isset($vo.month) and $vo.month eq 1}selected{/if}>星期二</option>
- <option value="wednesday" {if isset($vo.month) and $vo.month eq 1}selected{/if}>星期三</option>
- <option value="thursday" {if isset($vo.month) and $vo.month eq 1}selected{/if}>星期四</option>
- <option value="friday" {if isset($vo.month) and $vo.month eq 1}selected{/if}>星期五</option>
- <option value="saturday" {if isset($vo.month) and $vo.month eq 1}selected{/if}>星期六</option>
- <option value="sunday" {if isset($vo.month) and $vo.month eq 1}selected{/if}>星期日</option>
- </select>
- </div>
- </div>
- <div class="layui-form-item">
- <label class="layui-form-label">开始时间</label>
- <div class="layui-input-inline">
- <input type="time" name="begin_time" required="begin_time" value="{$vo.begin_time|default='00:00'}" class="layui-input">
- </div>
- </div>
- </div>
特别提示:每一个radio点击后需要切换的表单内容,需要用“form-group”包括起来。并赋予一个自定义的属性便于DOM选择器选择。上述示例中增加的属性是“data-storage-type”。
3、JS部分
- <script>
- (function () {
- window.form.render();
- buildForm('{$vo.type|default="day"}');
- $('[name=type]').on('click', function () {
- buildForm($('[name=type]:checked').val())
- });
- // 表单显示编译
- function buildForm(value) {
- var $tips = $("[data-storage-type='" + value + "']");
- $("[data-storage-type]").not($tips.show()).hide();
- }
- //按年下级radio切换
- buildYear('{$vo.year_type|default="week"}');
- $('[name=year_type]').on('click', function () {
- buildYear($('[name=year_type]:checked').val())
- });
- function buildYear(value){
- var $tips = $("[data-storage-type=year-" + value + "]");
- $("[data-storage-type]").not($tips.show()).hide();
- $("[data-storage-type='year']").show();
- //window.form.render(null,'year-'+value);
- }
- //按月下级radio切换
- buildMonth('{$vo.month_type|default="day"}');
- $('[name=month_type]').on('click',function (){
- buildMonth($('[name=month_type]:checked').val());
- })
- function buildMonth(value){
- var $tips = $("[data-storage-type=month-" + value + "]");
- $("[data-storage-type]").not($tips.show()).hide();
- $("[data-storage-type='month']").show();
- }
- })();
如果是只有一级切换,只需要一个buildForm即可。因为在具体案例中采用了多级radio选项,故而多写了两个,并且在多级radio点击切换的时候,还要保证第二级radio的显示。
至于下拉选择框,选择后切换表单内容可以依照上述示例进行简单修改即可实现。