//html部分
<a href="#" data-status="{$vo.state}" data-urid="{$vo.id}" onclick="edit_status(this)">
{if $vo.state=='1'}
<span class="span-btn">
启用
</span>
{elseif $vo.state=='2'/}
<span class="span-btn1">
禁用
</span>
{/if}
</a>
//ajax部分
//修改公开隐藏状态
function edit_status(_this){
var data_status = $(_this).attr('data-status');
var data_urid = $(_this).attr('data-urid');
$.ajax({
type:"post",
url:"{:url('Index/state')}",
data:{'urid':data_urid,'status':data_status},
success:function(data){
console.log(data);
if(data.data == 1){
$(_this).attr('data-status',1);
$(_this).text('启用').css({"background":"lightblue","display":"inline-block","width":"55px","border-radius":"5px","color":"#fff"});
}else{
$(_this).attr('data-status',2);
$(_this).text('禁用').css({"background":"red","display":"inline-block","width":"55px","border-radius":"5px","color":"#fff"});
}
}
})
}
//php部分
//状态
public function state(){
$param=input('param.');
// dump($param);exit;
if($param['status']==1){
$m=db('news')->where('id',$param['urid'])->update(['state'=>2]);
}elseif($param['status']==2){
$m=db('news')->where('id',$param['urid'])->update(['state'=>1]);
}else{
return json(['code'=>2,'message'=>'state不能为空']);
}
$state=db('news')->where(['id'=>$param['urid']])->value('state');
if($state == 1){
return json(['code'=>1,'message'=>'显示成功','data'=>$state]);
}else{
return json(['code'=>2,'message'=>'显示成功','data'=>$state]);
}
}