一、视图
<tr id="rm_<?php echo $v->category_id;?>">
<td><?php echo $v->category_id;?></td>
<td><?php echo $v->category_title;?></td>
<td><a href="/admin/event/category_edit/id/<?php echo $v->category_id;?>">修改</a> |
<a href="javascript:;" onclick="del(<?php echo $v->category_id;?>)">删除</a></td>
</tr>
<?php endforeach;?>
</table>
</div>
<script>
$(function(){
del = function (s){
var category_id = s;
//询问框
layer.confirm('您确认要删除吗?', {
btn: ['确认','取消']
}, function(){
$.post("/admin/event/category_del",{category_id:category_id},function(data){
if(data.msg == 1){
layer.msg('删除成功!', {icon: 1,time:1000});
$("#rm_"+category_id).remove(); //这里用remove删除,刷新页面影响分页
} else {
layer.msg('删除失败!', {icon: 2,time:1000});
}
},"json");
});
}
})
</script>
$.ajax({ type:"POST", url:"/admin/event/category_del", data:{category_id:category_id}, dataType:"json", success:function(data){ if(data.msg == 1){ layer.msg('删除成功!', {icon: 1,time:1000}); } else { layer.msg('删除失败!', {icon: 2,time:1000}); } } })
二、控制器
public function actionCategory_del() { $category_id = $_POST['category_id']; $model = Category::model()->findByPk($category_id); if($model->delete()){ echo json_encode(array('msg'=>1)); } }
三、其他 带图片删除
public function actionDel() { $mp_id = $_POST['mp_id']; $model = Mp::model()->findByPk($mp_id); if(is_file($model->img_url)){ unlink($model->img_url); } if(is_file($model->qrcode_url)){ unlink($model->qrcode_url); } if($model->delete()){ echo json_encode(array('msg'=>1)); } }