zoukankan      html  css  js  c++  java
  • ajax按钮改变数据状态

    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;
            }
        }

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

  • 相关阅读:
    64_l2
    64_l1
    64_k2
    64_k1
    64_j2
    64_j1
    64_g6
    64_g5
    64_g4
    64_g3
  • 原文地址:https://www.cnblogs.com/lamp01/p/9057385.html
Copyright © 2011-2022 走看看