删除的注意事项:点击删除按钮必须为class,不能为id
//注意删除按钮的自定义值data-id="{$vo['id']}"
<foreach name="shopdata" item="v">
<tr class="shopname" foodtype = "{$v.id}" name="first" status="none">
<td class="sort-caret" style="text-align:center;font-size:16px;">
{$v.shop_name}{$v['id']}
<span class="carets iconfont icon-cgxialatubiao"></span>
</td>
<td><a shopid="{$v['id']}" class="btn btn-primary couponadd" >添加优惠卷</a></td>
</tr>
<foreach name="v['coupondata']" item="vo">
<tr style="display: none" class="category second_{$v.id} all_{$v.id}" foodtype ="{$v.id}" name="second" id="{$vo.id}" status="none">
<td class="sort-caret" style="text-align:center;font-size:16px;">
{$vo.coupon_name}
</td>
<td style="text-align:center;">
{$vo['coupon_denomination']}
</td>
<td>{$vo['coupon_num']}</td>
<td>{$vo['coupon_num']}</td>
<td>{$vo['start_time']|date="Y-j-n",###}</td>
<td>{$vo['end_time']|date="Y-j-n",###}</td>
<td>是</td>
<td style="text-align:center;">
<a couponId="{$vo['id']}" class="btn btn-primary btn-sm icon-edit couponedit">修改</a>
<a data-id="{$vo['id']}" class="btn btn-danger btn-sm icon-delete coupondelete">删除</a>
</td>
</tr>
</foreach>
</foreach>
//js代码
$('.coupondelete').click(function(){
var that = $(this);
if(confirm('您确定要删除')) {
var data = {'id':that.data('id')}
$.ajax({
url:"{:U('/addon/WeiuidDeit/web/foodcouponDelect')}",
type:'POST',
data:data,
dataType:'json',
success:function(data){
data = JSON.parse(data);
if(data.error == 200){
//直接移除父级的标签
that.parents('tr').remove();
}
alert(data.msg);
},
error:function(){alert('删除出错')}
})
}
})
后台控制器
// 优惠卷删除
public function foodcouponDelect()
{
$id = $_POST['id'];
if (M('foodcoupon')->delete($id)) {
$code['msg'] = '删除成功';
$code['error'] = '200';
} else {
$code['msg'] = '删除失败';
$code['error'] = '400';
}
$this->ajaxReturn(json_encode($code));
}