在页面上定义一个按钮
<button type="button" class="btn"><a href="JavaScript:;" class="id" b_id="{{$attachment['id']}}">删除</a></button>
js代码
<script>
$('.id').click(function (){
var b_id = $(this).attr('b_id');
console.log(b_id);
var that = $(this);
$.ajax({
url:"{{admin_route('contract_del')}}",//接口路由
data:{id:b_id},//传值
type:'get',
dataType:'json',
success:function (res){
console.log(res.code)
if(res.code == '200'){
that.parent().parent().remove();
//我的按钮因为是有两个dive包裹,所以有两层parent
}
}
})
})
</script>
完整页面按钮如下图:
接口代码
public function contract_del()
{
$id = \request()->get('id');
$delRes = Attachment::where('id',$id)->delete();
if($delRes){
echo json_encode(['code'=>'200','msg'=>'删除成功','data'=>$delRes]);
}
}
我的路由
$router->get('sale-order/contract_del',[ App\Admin\Controllers\SaleOrderController::class,'contract_del' ])->name('contract_del');