jquery+php实现节点添加和异步删除
<div class="formbody"> <div class="formtitle"><span>商品相册</span></div> <li style="border: 1px solid grey;margin-bottom: 20px;"> {volist name="data" id="vol"} <span><img src="{$vol.pics_mid|ltrim='.'}" width="178"><a href="javascript:;" class="remove" data="{$vol.pics_id}">[-]</a> </span> {/volist} </li> <form action="" method="post" enctype="multipart/form-data"> <ul class="forminfo"> <li> <label>商品图片[<a href="javascript:;" class="add">+</a>]</label> <input name="goods_pic[]" type="file" /> </li> <li> <label> </label> <input name="" id="btnSubmit" type="button" class="btn" value="确认保存" /> </li> </ul> </form> </div> <script type="text/javascript"> $(function(){ //表单提交 $('#btnSubmit').on('click',function(){ $('form').submit(); }) //点击添加取消 $('.add').click(function(){ var li="<li><label>商品图片[<a href='javascript:;' class='del'>-</a>]</label><input name='goods_pic[]' type='file' /></li>"; $(this).parent().parent().after(li); }); $('.del').live('click',function(){ $(this).parent().parent().remove(); }); //异步删除 $('.remove').click(function(){ var pics_id=$(this).attr('data'); var _this=$(this); $.ajax({ type: "get", data: "pics_id="+pics_id, url: "{:url('photos_del')}", dataType:"json", success: function(data) { console.log(data); if(data==1){ _this.parent().remove(); }else{ alert('删除失败'); } }, }); }); }); </script>
<?php public function photos_del($pics_id){ $Goodspics=model('Goodspics'); $data=$Goodspics->find($pics_id); $ab_url=ROOT_PATH . 'public'; @unlink($ab_url. $data['pics_big']); @unlink($ab_url. $data['pics_mid']); @unlink($ab_url. $data['pics_sma']); $result=$Goodspics->where('pics_id',$pics_id)->delete(); return $result ? 1 : 0; }
参考地址 https://www.cnblogs.com/keyi/p/5817728.html
希望广大博友给予建议和指导