選中要刪除的商品,點擊批量刪除javascript
<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>
按鈕源碼html
<a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i class="Hui-iconfont"></i> 批量刪除</a>
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"; } }
$("input[name='ids']:checked").each(function(i){java
checkID[i] = $(this).val();
});
2.如何在刪除以後不刷新頁面能達到刪除的效果,操做dom元素jquery
$ids.each(function(i){ $(this).parents("tr").remove(); // console.log($(this).parents("tr")); });
3.tp5批量刪除的語法。data是一個數組。ajax
$res=$DB::table("goods")->delete($data);