选中要删除的商品,点击批量删除
- 先在控制器使用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);
黄山市民网:https://www.huangshanshimin.com/