2017-03-18 100 views
0

我試圖刪除一個錶行,我的按鈕工作,我的產品從數據庫中刪除,但它不會刪除頁面上,直到刷新,我總是得到「它失敗「即使是它的工作......我做錯了什麼?似乎沒有被稱爲「成功」。用ajax調用刪除tr問題

$(".deleteProduct").click(function(){ 
    var id = $(this).data("id"); 
    var token = $(this).data("token"); 
    $.ajax(
      { 
       url: "/eventlineitem/"+id, 
       type: 'DELETE', 
       dataType: "JSON", 
       data: { 
        "id": id, 
        "_method": 'DELETE', 
        "_token": token 
       }, 
       success: function() 
       { 
        console.log("it Work"); 
       } 
      }); 

    console.log("It failed"); 
}); 

這裏是我的html

<tr class="item{{$item->id}}"> 
    <td class="align-center" scope="row">{{$item->product->id}}</td> 
    <td class="align-center">{{$item->quantity}}</td> 
    <td><a href="/products/{{$item->product->id}}">{{$item->product->name}}</a></td> 
    <td class="align-center">{{$item->warehouse_id}}</td> 
    <td class="align-center">{{$item->product->location}}</td> 
    <td><div class="switch"> 
    <label><input type="checkbox"><span class="lever switch-col-green"></span></label> 
    </div></td> 
    <td><i class="material-icons deleteProduct" data-id="{{ $item->id }}" data-token="{{ csrf_token() }}">delete</i></td> 
</tr> 
+0

'type:'DELETE'' is valid? – Slime

+0

是啊我猜大聲笑,當我按我的該行從數據庫中刪除 – DevJustin

+0

學習新的東西我猜,https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 – Slime

回答

0

我不知道你是怎麼預期的HTML知道你刪除的行?

我想你應該創建一個變量的AJAX調用之前

var target = $(this).parents("tr"); 

然後在ajax.success通話

target.remove() 
+0

我認爲目標是在這裏。雖然我們可以做target.parents('tr')。remove() –

+0

好的。謝謝我會更新 – stackoverfloweth

+0

我試過了,仍然是和以前一樣的問題。它從數據庫中被刪除我得到一個狀態「OK」/ 200 – DevJustin

0

更改成功函數這樣

success: function() 
       { 
        $(".item"+id).remove(); 
        console.log("it Work"); 
       } 
+0

我試過了,仍然是和以前一樣的問題。 – DevJustin

0
success: function() { 
    $(this).parent().parent().remove(); 
    console.log("it Work"); 
} 

B就這樣,如果console.log被解僱了,那麼你就知道成功了。

+0

我仍然得到「失敗」,仍然從數據庫中刪除,當頁面刷新時,該行不見了。 – DevJustin

+0

控制檯是否記錄任何東西?你確認成功被解僱嗎? – Slime

+0

控制檯日誌「失敗」,我得到的狀態是OK/200(使用laravel devtools) – DevJustin

0

你可以嘗試用下面的代碼:

$(".deleteProduct").click(function(){ 
    var _selector = $(this); 
    var id = _selector.data("id"); 
    var token = _selector.data("token"); 

    $.ajax({ 
     url: "/eventlineitem/"+id, 
     type: 'DELETE', 
     dataType: "JSON", 
     data: { 
      "id": id, 
      "_method": 'DELETE', 
      "_token": token 
     }, 
     success: function(){ 
      console.log("it Work"); 
      _selector.parents('tr.item' + id).remove(); 
     } 
    }); 
}); 

讓我知道,如果你的工作。

+0

沒有這個代碼不起作用,是的我的產品從數據庫中刪除,但並沒有從dom中刪除它。我用它來手動回覆laravel的消息,而不是使用狀態碼,然後使用刪除 – DevJustin