layui switch 开关监听 动态修改 switch 值
HTML:
<div class="layui-form-item"> <label class="layui-form-label">是否开启</label> <div class="layui-input-inline"> <div class="layui-row"> <input type="checkbox" name="switch" lay-skin="switch" lay-filter="status" lay-text="是|否"> </div> </div>
JS:
layui.use(['form','jquery'], function(){ var form = layui.form ,layer = layui.layer ,$ = jquery; //监听指定开关 form.on('switch(status)', function(data){ console.log(data); if(this.checked){ console.log('开'); }else{ console.log('关'); } }); });
动态修改 switch 值:
//修改原始input的checked状态, 重新渲染 <input type="checkbox" name="xxx" lay-skin="switch" lay-filter="flag"> layui.jquery('input[name="xxx"]').attr('checked', 'checked'); //改变开关为 开 layui.jquery('input[name="xxx"]').removeAttr('checked'); //改变开关为 关 //重新渲染 layui.form().render('checkbox');