10

我正在尝试升级我的 JavaScriptconfirm()操作以使用SweetAlert。目前我的代码是这样的:

<a href="/delete.php?id=100" onClick="return confirm('Are you sure ?');" >Delete</a>

这会在导航到删除页面之前等待用户确认。我想使用 SweetAlert 中的这个例子来要求用户在删除之前确认:

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) {     
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
  } 
  else {     
    swal("Cancelled", "Your imaginary file is safe :)", "error");  
  } 
});


我尝试过的一切都失败了。当显示第一个警报时,页面已经继续并删除了该项目并在用户单击警报按钮之前刷新。如何让页面等待用户输入?

任何帮助将不胜感激。

4

4 回答 4

6

您不能将其用作confirm. confirm阻止单线程执行,直到对话框被确认,您不能使用基于 JavaScript/DOM 的对话框产生相同的行为。

您需要在警报框/delete.php?id=100成功回调中发出请求。

代替...

swal("Deleted!", "Your imaginary file has been deleted.", "success");

你需要

<a href="#">Delete<a>
...

$.post('/delete.php?id=100').then(function () {
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
});

您还必须修复您delete.php的仅接受POST请求。允许GET请求删除资源是个大问题。当 Google 或任何其他爬虫第一次找到您的页面时,它会查看href您文档中每个链接的 并跟踪每个链接,删除您的所有内容。他们不会被confirm盒子阻止,因为他们可能(除了谷歌)不会评估任何 JavaScript。

于 2015-01-16T15:42:44.097 回答
5

你可以这样做。

HTML:

<a href="/delete.php?id=100" class="confirmation" >Delete</a>

JS:

$('.confirmation').click(function (e) {
    var href = $(this).attr('href');

    swal({
        title: "Are you sure?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "No, cancel plx!",
        closeOnConfirm: true,
        closeOnCancel: true
    },
            function (isConfirm) {
                if (isConfirm) {
                    window.location.href = href;
                }
            });

    return false;
});

似乎是一个黑客,但它对我有用。

于 2016-03-31T11:34:07.647 回答
0
$('.delete').click(function () {
var id = this.id;
swal({
  title: "Are you sure?",
  text: "Your will not be able to recover this post!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
},
function(){
   alert(id);
});
});


<a id="<?php echo $row->id_portfolio ?>" class=" delete">
于 2015-05-17T12:59:44.553 回答
0

这是 Angular 指令中的一个示例(因为 SweetAlert 是通过 Angular 指令包装器提供的)。这是在 JavaScript 中执行此操作的一种“优雅”方法。在点击事件上,有一个 e.stopImmediatePropagation(),然后如果用户确认,它会评估“ng-click”函数。(注意 scope.$eval 不是 JavaScript eval())。

标记: <i class="fa fa-times" ng-click="removeSubstep(step, substep)" confirm-click="Are you sure you want to delete a widget?"></i>

简单的“确认点击”指令:

/**
 * Confirm click, e.g. <a ng-click="someAction()" confirm-click="Are you sure you want to do some action?">
 */
angular.module('myApp.directives').directive('confirmClick', [
  'SweetAlert',
  function (SweetAlert) {
    return {
      priority: -1,
      restrict: 'A',
      link: function (scope, element, attrs) {
        element.bind('click', function (e) {
          e.stopImmediatePropagation();
          e.preventDefault();

          var message = attrs.confirmClick || 'Are you sure you want to continue?';

          SweetAlert.swal({
            title: message,
            type: 'warning',
            showCancelButton: true,
            closeOnConfirm: true,
            closeOnCancel: true
          }, function (isConfirm) {
            if (isConfirm) {
              if(attrs.ngClick) {
                scope.$eval(attrs.ngClick);
              }
            } else {
              // Cancelled
            }
          });
        });
      }
    }
  }
]);
于 2016-11-04T16:49:21.503 回答