ajax 删除数据无刷新

//html页面

<!doctype html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
</head>

<body>
{include file="header"}
<table class="weui-table weui-border-tb" id="cusTable">
<thead>
<tr>
<th>编号</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{volist name="data" id="vo"}
<tr>
<td data-field="" id="sub" >{$vo.id}</td>

<th>
<a title="编辑" href="{:url('Index/list_edit',['id'=>$vo.id])}" id="edit" data-id="{$vo.id}">
<i class="layui-icon">修改</i>
</a>
&nbsp;&nbsp;&nbsp;
</a> -->
<a href="###" id="{$vo.id}" onclick="del(this)">
<i class="layui-icon">删除 </i>
</a>

</th>
</tr>
{/volist}
</tbody>
</table>

<div class="pages">{$page}</div>

<!--form validation js -->
<script src="__STATIC__/js/common/jquery.js"></script>
<script src="__STATIC__/js/parsley.js"></script>
<script src="__STATIC__/js/zh_cn.js"></script>
<script src="__STATIC__/layui/layui.js"></script>

<script>

//删除
function del(_this){
var id = $(_this).attr('id');
var del=window.confirm("您确定要删除吗?");
if(del){
$.ajax({
type:"DELETE",
url:"{:url('Index/list_del')}",
data:{'id':id},
success:function(m){
if(m.code==1){
$(_this).parent().parent().remove();//删除无刷新    重要
}else{
alert('删除失败');
}
}
})
}

}

</script>

</body>
</html>

//php

//删除
public function list_del()
{
$list=db('news')->where('id',input('id'))->find();
//判断是否有要删除的图片
for ($i=1; $i <3 ; $i++) {
$img='img'.$i;
if(!empty($list[$img])){
$filename = ROOT_PATH . 'public' . DS . 'uploads/'.$list[$img];
// dump($filename);exit;
if(file_exists($filename)){
unlink($filename);
}
}
}
$m=db('news')->where('id',input('id'))->delete();
if($m>0){
return json(['code'=>1,'message'=>'删除成功']);
}else{
return json(['code'=>1,'message'=>'删除成功']);
}
}

上一篇:集群下Cookie共享,必须要设置machineKey


下一篇:Node.js Express 获取request原始数据