# 异步修改状态值 准备条件 http://www.layui.com/ 粘贴代码开始 html前端部分 ~~~ <td class="layui-form"> <font color="green"><strong> <input type="checkbox" {if condition="$vo.open==1"} checked{/if} name="switch" lay-skin="switch" data-urls='{:url('Spcpackage/updatestatus',['id'=>$vo['id']])}' lay-text="开启|关闭"> </strong></font> </td> ~~~ 后台控制器部分 ~~~ function updatestatus($id){ Db('Spcpackage')->where(['id' => $id])->update(['open'=>input('open')]); $info=Db('Spcpackage')->where(['id' => $id])->find(); if ($info['open']==1){ $this->success('正在开启'); }else{ $this->error('正在关闭'); } } ~~~ layui+js部分 ~~~ layui.use(['form'], function(){ var form = layui.form //监听指定开关 form.on('switch', function(data){ var url=this.getAttribute('data-urls'); var open=this.checked ? '1' : '0' //询问框 layer.confirm('您确定要更改吗?', { btn: ['确定','取消'] //按钮 }, function(){ $.get(url,{open:open},function(info){ if(info.code === 1){ setTimeout(function () {location.href = info.url;}, 1000); } layer.msg(info.msg); }); }, function(){}); }); }); ~~~