簡體   English   中英

如何使用甜蜜提醒來確認(然后提交)?

[英]How to use confirm (then submit) using sweet alert?

我有一個表格,其中每一行都有此表格的刪除按鈕

<form id="tableDelete_1">
 <input type="hidden" name="tipo" value="delete" />
 <input type="submit" name="send" value="Delete" class="btn btn-danger btn-xs" onClick="return confirm(`Are you sure?`)" />
</form>

<form id="tableDelete_2">
 <input type="hidden" name="tipo" value="delete" />
 <input type="submit" name="send" value="Delete" class="btn btn-danger btn-xs" onClick="return confirm(`Are you sure?`)" />
</form>

然后在頁面底部

$(document).on('submit', '[id^=tableDelete_]' , function() { 
 return callAjax( $(this).serialize() ); 
});

function callAjax( data ) {
  $.ajax({
   type : 'POST',
   url  : 'call/page.php',
   data : data,
   success :  function(data) { ... },
   error: function(data) { ... }
  });
  return false;
 };

現在我要刪除經典

onClick="return confirm(`Are you sure?`)"

並使用sweetalert ...當我只想顯示具有此功能的彈出窗口時,我剛開始遇到問題

$(document).on('submit', '[id^=tableDelete_]' , function() { 
 swal({
  title: "Are you sure?",
  text: "You will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
 },
 function(){
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
 });
};

彈出窗口僅顯示一秒鍾,然后重新加載頁面,因為我認為表單已提交...

請給我幫忙

如果我沒看錯,我認為您正在尋找類似的東西?

$(document).on('submit', '[id^=tableDelete_]', function (e) {
  e.preventDefault();

  var data = $(this).serialize();

  swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: "No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
  },
    function (isConfirm) {
      if (isConfirm) {
        $.ajax({
          type: 'POST',
          url: 'call/page.php',
          data: data,
          success: function (data) {
            swal("Deleted!", "Your imaginary file has been deleted.", "success");
          },
          error: function (data) {
            swal("NOT Deleted!", "Something blew up.", "error");
          }
        });
      } else {
        swal("Cancelled", "Your imaginary file is safe :)", "error");
      }
    });

  return false;
});

SweetAlert使用promise進行確認回調:

swal({
      title: "Confirm?",
      text: "Are you sure?",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Confirm",
      cancelButtonText: "Back"
      }
    ).then(
      function (isConfirm) {
        if (isConfirm) {
          console.log('CONFIRMED');
        }
      },
      function() {
         console.log('BACK');
      }
    );
$(document).on('submit', '[id^=tableDelete_]' , function(e) { 
e.preventDefault();
//do your popup stuff
return false
});

您需要防止在事件(作為e傳遞)發生時發生的默認事件。

暫無
暫無

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

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