html部分:
<div class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">开关</label>
<div class="layui-input-block"> <input class="test" type="checkbox" name="switch" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF"> </div>
</div>
</div>
JS部分
layui.use(['form'], function(){
var form = layui.form
,layer = layui.layer
//监听指定开关
form.on('switch(switchTest)', function(data){
if(this.checked){ //this.checked就是值
layer.msg('开关checked:'+ (this.checked ? 'true' : 'false'), {
offset: '6px'
});
}else{
layer.msg('开关: 关掉了', {
offset: '6px'
});
}
//do some ajax opeartiopns;
});
});
// layui.jquery('.test').removeAttr('checked'); //改变开关为 关
// layui.jquery('.test').attr('checked','checked'); //改变开关为 开
//重新渲染
layui.form.render(); // 重新渲染。。。