HTML代碼:
<input type="checkbox" lay-skin="switch" {$vo.state? 'checked=""': ''} lay-filter="switch" data-url="{:url('staychoice',['id'=>$vo['id']])}" lay-text="顯示|顯示">
監(jiān)聽代碼:
<script>
layui.use('element', function(){
var element = layui.element;
});
layui.use('form',function(){
var form = layui.form;
var $ = layui.jquery;
form.on('switch(switch)', function(data) {
var url = $(this).data('url');
var state = data.elem.checked;
if(state) {
state = 1;
} else {
state = 0;
}
$.post(url, {state:state}, function(res) {
if(res.code == 1) {
layer.msg(res.msg);
} else {
layer.msg(res.msg);
}
});
return false;
});
})
</script>
請求邏輯代碼:
public function StayChoice()
{
if (Request()->isPost()) {
$data['id'] = $this->request->param('id');
$data['state'] = $this->request->param('state');
$result = Db::name('test')->update($data);
if ($data['state']) {
$msg = '前臺顯示';
}else{
$msg = '取消顯示';
}
$res['code'] = 1;
$res['msg'] = $msg.'成功譬巫!';
if ($result === false) {
$res['code'] = 0;
$res['msg'] = $msg.'失敗';
}
return json($res);
}else{
$res['code'] = 0;
$res['msg'] = '很意外,沒有收到param值';
return json($res);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者