tp5--ajax删除

 //点击删除获取此条数据id并弹出确认框
    $('.del').click(function(){
      var id =$(this).data('id');
      // 弹窗
      layer.confirm('确认删除吗?',{
        icon:3,
        title:'提示',
      },function(index){
        //jq的ajax
        $.ajax({
          url:'{:url("index/del")}',//路径地址
          data:{id:id},
          type:'post',
          dataType:'json',
          success:function(res){
            //根据返回值页面处理
            if(res.code==1){
              layer.msg(res.msg);
              setTimeout(function(){
                // location.reload();
                location.href='{:url("index/plist")}';//自动刷新
              },500);
            }else{
              layer.msg(res.msg);
            }
          }
        })
        // layui弹窗关闭
        layer.close(index);
      })
    })
 
/**
     * 删除
     */
    public function del(){
        if(request()->isAjax()){//判断是否是ajax
            $id=input('id');//接受变量
            $res=db('products')->delete($id);//删除语句
            $arr=[];//空数组
            if($res){
                $arr['code']=1;
                $arr['msg']='删除成功';
            }else{
                $arr['code']=2;
                $arr['msg']='删除失败,请稍后再试';
            }
            return $arr;//自动转换json
        }
    }
posted @ 2020-09-21 17:02  summer°  阅读(165)  评论(0编辑  收藏  举报