删除的注意事项:点击删除按钮必须为class,不能为id

    1. //注意删除按钮的自定义值data-id="{$vo['id']}"
    2. <foreach name="shopdata" item="v">
    3. <tr class="shopname" foodtype = "{$v.id}" name="first" status="none">
    4. <td class="sort-caret" style="text-align:center;font-size:16px;">
    5. {$v.shop_name}{$v['id']}
    6. <span class="carets iconfont icon-cgxialatubiao"></span>
    7. </td>
    8. <td><a shopid="{$v['id']}" class="btn btn-primary couponadd" >添加优惠卷</a></td>
    9. </tr>
    10. <foreach name="v['coupondata']" item="vo">
    11. <tr style="display: none" class="category second_{$v.id} all_{$v.id}" foodtype ="{$v.id}" name="second" id="{$vo.id}" status="none">
    12. <td class="sort-caret" style="text-align:center;font-size:16px;">
    13. {$vo.coupon_name}
    14. </td>
    15. <td style="text-align:center;">
    16. {$vo['coupon_denomination']}
    17. </td>
    18. <td>{$vo['coupon_num']}</td>
    19. <td>{$vo['coupon_num']}</td>
    20. <td>{$vo['start_time']|date="Y-j-n",###}</td>
    21. <td>{$vo['end_time']|date="Y-j-n",###}</td>
    22. <td>是</td>
    23. <td style="text-align:center;">
    24. <a couponId="{$vo['id']}" class="btn btn-primary btn-sm icon-edit couponedit">修改</a>&nbsp;
    25. <a data-id="{$vo['id']}" class="btn btn-danger btn-sm icon-delete coupondelete">删除</a>
    26. </td>
    27. </tr>
    28. </foreach>
    29. </foreach>
    30. //js代码
    31. $('.coupondelete').click(function(){
    32. var that = $(this);
    33. if(confirm('您确定要删除')) {
    34. var data = {'id':that.data('id')}
    35. $.ajax({
    36. url:"{:U('/addon/WeiuidDeit/web/foodcouponDelect')}",
    37. type:'POST',
    38. data:data,
    39. dataType:'json',
    40. success:function(data){
    41. data = JSON.parse(data);
    42. if(data.error == 200){
    43. //直接移除父级的标签
    44. that.parents('tr').remove();
    45. }
    46. alert(data.msg);
    47. },
    48. error:function(){alert('删除出错')}
    49. })
    50. }
    51. })

    后台控制器

    1. // 优惠卷删除
    2. public function foodcouponDelect()
    3. {
    4. $id = $_POST['id'];
    5. if (M('foodcoupon')->delete($id)) {
    6. $code['msg'] = '删除成功';
    7. $code['error'] = '200';
    8. } else {
    9. $code['msg'] = '删除失败';
    10. $code['error'] = '400';
    11. }
    12. $this->ajaxReturn(json_encode($code));
    13. }