簡體   English   中英

Laravel:確認刪除不起作用

[英]Laravel: Confirmation deletion does not work

我在刪除確認時遇到問題; 具體來說,當我單擊刪除按鈕時,該項目被直接刪除,而沒有首先出現確認刪除的 window。

有人能幫我嗎?

 function confirmDelete(){ let deleteLink = document.querySelector('.delete'); deleteLink.addEventListener('click', function(event) { event.preventDefault(); let choice = confirm(this.getAttribute('data-confirm')); if (choice) { window.location.href = this.getAttribute('action'); } }); }
 <form action="{{ route('client.destroy',compact('client'))}}" method="POST"> <a class="btn btn-primary" href="{{route('client.edit', compact('client'))}}">Edit</a> @csrf @method('DELETE') <button type="submit" class="btn btn-danger delete" onclick="confirmDelete()" data-confirm="Sure?">Delete</button> </form>

這是解決方案。

設置按鈕類型按鈕並將id設置為form。

<form action="{{ route('client.destroy',compact('client'))}}" method="POST" id="form_id">

  <a class="btn btn-primary" href="{{route('client.edit',compact('client'))}}">Edit</a>

@csrf
@method('DELETE')

  <button type="button" class="btn btn-danger delete" onclick="confirmDelete()" data-confirm="Sure?">Delete</button>
</form>

您的 function 發生了一些變化。

function confirmDelete() {
  let deleteLink = document.querySelector('.delete');

  deleteLink.addEventListener('click', function (event) {
    event.preventDefault();

    let choice = confirm($(this).data('confirm'));

    if (choice) {
      $('#form_id').submit();
      // window.location.href = this.getAttribute('action');
    }
  });
}

如果您為多個客戶執行此操作,請確保 FORM ID 是唯一的。

您應該防止輸入的默認行為(即提交表單 onclick):

onclick="event.preventDefault();confirmDelete()"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM