选中要删除的商品,点击批量删除
- 先在控制器使用sql语句查出商品信息goods
- 然后在html源码中使用goods变量。
<table> {foreach $goods as $item} <tr> <td><input name="ids" class="ids" type="checkbox" value="{$item.goods_id}"></td> <td>123</td> <td>2324</td> </tr> {/foreach} </table>
按钮源码
<a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i class="Hui-iconfont"></i> 批量删除</a>
- js
function datadel(){ $ids = $("input[name='ids']:checked"); var checkID=[]; $("input[name='ids']:checked").each(function(i){ checkID[i] = $(this).val(); }); //判断数组是否为空。空的话禁止点击 if(checkID.length == 0){ return; } // console.log(checkID); layer.confirm('确认要删除吗?',function(index){ // $ids = $(".ids"); $.ajax({ type: 'POST', url:"{:url('productBatchDelAjax')}", data:{checkID:checkID}, dataType: 'json', success: function(data){ // alert(data); // $(obj).parents("tr").remove(); $ids.each(function(i){ $(this).parents("tr").remove(); // console.log($(this).parents("tr")); }); layer.msg('已删除!',{icon:1,time:1000}); }, error:function(data) { console.log(data.msg); }, }); }); }
- 控制器异步请求删除数据
public function productBatchDelAjax() { $data = $_POST['checkID']; $DB = new Db; $res=$DB::table("goods")->delete($data); if($res){ echo"ok"; } }
- 注意:
- jquery如何把选中的id提交到后台
$("input[name='ids']:checked").each(function(i){
checkID[i] = $(this).val();
});
2.如何在删除之后不刷新页面能达到删除的效果,操作dom元素
$ids.each(function(i){ $(this).parents("tr").remove(); // console.log($(this).parents("tr")); });
3.tp5批量删除的语法。data是一个数组。
$res=$DB::table("goods")->delete($data);