2016-12-15 292 views
1

我正在使用SweetAlert和Symfony,我希望用戶在完成刪除操作之前進行確認。Sweet Alert不會等到用戶點擊確定

發生什麼事情是當用戶點擊刪除按鈕SweetAlert彈出,然後立即消失,項目被刪除。

我不希望項目被刪除,直到用戶從SweetAlert彈出窗口中單擊確定。

這是我的設置。

JS:

function myFunction() { 
     swal({ 
      title: "Are you sure?", 
      text: "You will not be able to recover this Job Title!", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#DD6B55", 
      confirmButtonText: "Yes, delete it!", 
      closeOnConfirm: false, 
      closeOnCancel: false 
     }, function (isConfirm) { 
      if (isConfirm){ 
       swal("Deleted!", "Job Title has been deleted.", "success"); 
      } else { 
       swal("Cancelled", "Job Title was not deleted.", "error"); 
      } 
     }); 
    }; 

HTML:

<a href="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}" onclick="myFunction()" class="btn btn-white" data-toggle="tooltip" data-toggle="tooltip" data-placement="top" title="Delete"> 
    <i class="fa fa-trash"></i> 
</a> 

我如何刪除操作等待,直到用戶點擊OK?

編輯:我結束了使用$.post和移動hrefdata-這樣的:

<a href="#" onclick="myFunction(this)" data-url="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}" class="btn btn-white" data-toggle="tooltip" data-toggle="tooltip" data-placement="top" title="Delete"> 
                   <i class="fa fa-trash"></i> 
                  </a> 
function myFunction(btn) { 
     var url = $(btn).data('url'); 
     swal({ 
      title: "Are you sure?", 
      text: "You will not be able to recover this Job Title!", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#DD6B55", 
      confirmButtonText: "Yes, delete it!", 
      closeOnConfirm: false 
     }, function() { 
      $.post(url, function(){ 
       swal("Deleted!", "Job Title has been deleted.", "success"); 
       $(btn).parents('tr').remove(); 
      }); 
     }); 
    }; 
+1

它與Symfony無關。你必須防止默認的瀏覽器行爲(通過返回false onclick即) – Brewal

+0

所以我改變了我的onclick這個:'myFunction();返回false;'現在不刪除任何東西。我可以點擊確定,但不會刪除。 – iamthestreets

+0

你在哪裏閱讀sweetalert允許你刪除任何東西?這只是簡單的js警報功能漂亮的包裝。你可以在你的'isConfirm'函數中添加ajax調用來刪除Symfony後端的操作。 – malcolm

回答

-1

一個更容易的解決方案是直接在onclick添加函數,像這樣:

<a href="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}" 
    onclick="return confirm('are u sure?')" class="btn btn-white" 
    data-toggle="tooltip" data-toggle="tooltip" data-placement="top" title="Delete"> 
    <i class="fa fa-trash"></i> 
</a> 

我使用這個,如果你點擊確定,它將只進入路線。 很簡單 - >保持代碼簡單。

編輯#2

如果你改變你的功能,像這樣:

function myFunction() { 
    swal({ 
     title: "Are you sure?", 
     text: "You will not be able to recover this Job Title!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }, function() { 
     swal("Deleted!", "Job Title has been deleted.", "success"); 
    }); 
}; 

有什麼區別?

+0

是的,這適用於我,但我使用SweetAlert。我不想使用標準的JS警報。 – iamthestreets

+0

我是SweetAlert的新手。我可以看到你爲什麼想要使用它。試試我添加的編輯#2更改。 –

+0

立即刪除並關閉彈出窗口。感謝您的幫助順便說一句。 – iamthestreets

0

你可以做如下:

<a href="/delete/me" class="confirm" data-title="Want to delete this?" data-type="warning">Click me</a> 

<a href="/edit/me" class="confirm" data-title="Info image" data-type="info">Click me</a> 

和JS部分,需要的jQuery加載

$(function() { 
    $('a.confirm').click(function (e) { 
      e.preventDefault(); 
      var tthis = $(this); 
      swal({ 
       title: "Are you sure?", 
       text: $(this).data('title'), 
       type: $(this).data('type'), 
       showCancelButton: true, 
       confirmButtonText: "Yes", 
       cancelButtonText: "No", 
      }, function (isConfirm) { 
       if (isConfirm) { 
        document.location.href = tthis.attr('href'); 
       } 
      }); 
     }); 
}); 

要等待用戶確認您必須取消鏈接行動e.preventDefault();

然後手動去鏈接一旦用戶確認。

相關問題