码迷,mamicode.com
首页 > Web开发 > 详细

ajax按钮改变数据状态

时间:2018-05-18 19:22:08      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:OLE   on()   where   lse   admin   strong   切换   exist   func   

1、html代码

<td>
@if($project->done_deal==0)
<button type="button" class="btn btn-danger btn-xs active done"  uid="{{$project->id}}" status="{{$project->done_deal}}">否</button>
@else
<button type="button" class="btn btn-primary btn-xs active done"  uid="{{$project->id}}" status="{{$project->done_deal}}">是</button>
@endif
</td>

2、js代码

$(".done").click(function() {
        var id=$(this).attr(uid);
        var status=$(this).attr(status);
        var _this=$(this);
        if(status==1){
            var sta=0;
        }else{
            var sta=1;
        }
        var url="{{url(‘/admin/fine/done‘)}}";
        $.ajax({
            type: get,
            url: url+/+id+/+status,
            dataType:"json",
            data: {
            },
            success: function(data){
                console.log(data);
                if (data==200) {
                    _this.removeClass(btn-danger).addClass(btn-primary).html();
                    _this.attr(id,id);
                    _this.attr(status,sta);
                }else{
                    _this.removeClass(btn-primary).addClass(btn-danger).html();
                    _this.attr(uid,id);
                    _this.attr(status,sta);
                }
            }
        });
    });

3、php代码

public function done($id,$status)
    {
        $if_done=$status==1?0:1;
        Fine::where(id, $id)->update([done_deal=>$if_done]);
        //删除redis
        if(Redis::exists(fine_project.$id)){
            Redis::del(fine_project.$id);
        }
        if($if_done==1){
            return 200;
        }else{
            return 400;
        }
    }

 

点击按钮就可以直接切换数据的状态了

ajax按钮改变数据状态

标签:OLE   on()   where   lse   admin   strong   切换   exist   func   

原文地址:https://www.cnblogs.com/lamp01/p/9057385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!