繁体   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