sweetAlert preventDefault 和 return true

sweetAlert preventDefault and return true

我试过 sweeAlert 插件,效果很好,但我不知道确认后如何做默认的东西。

$(document).ready(function () {
function handleDelete(e){
    e.preventDefault();
    swal({
        title: "Are you sure?",
        text: "You will not be able to recover the delaer again!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete!",
        closeOnConfirm: false
    },
    function (isConfirm) {
        if (isConfirm) {
            return true;
        }
    });
};
});

和按钮

 <a href="{plink delete! $row->id_dealers}" class="delete" onclick"handleDelete(event);">&nbsp;</a>
 //{plink delete! $row->id_dealers} Nette -> calls php delete handler

我也试过 unbind()off() 而不是 return false,但没有用。 之前我在 onclick 属性中使用 confirm()return truereturn false,它有效,但看起来很糟糕。

你可以试试这个

$(document).ready(function () {
  $('.delete').on('click',function(e, data){
    if(!data){
      handleDelete(e, 1);
    }else{
      window.location = $(this).attr('href');
    }
  });
});
function handleDelete(e, stop){
  if(stop){
    e.preventDefault();
    swal({
      title: "Are you sure?",
      text: "You will not be able to recover the delaer again!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete!",
      closeOnConfirm: false
    },
    function (isConfirm) {
      if (isConfirm) {
        $('.delete').trigger('click', {});
      }
    });
  }
};

这里有演示http://jsbin.com/likoza/1/edit?html,js,output

另一种方法是使用表格代替 href

标记看起来像这样

<form action="">
  <input type="submit" ...... />
</form>

而不是 window.location = $(this).attr('href'); 你可以只说 form.submit()


更新

如果页面上有多个元素那么触发器可以这样使用

$(e.target).trigger('click', {});

这是一个演示 http://output.jsbin.com/likoza/2

我是这样做的:

$('.delete').on('click', function(e) {
    e.preventDefault();
    var currentElement = $(this);

    swal({
            title: "Are you sure?",
            text: "You will not be able to recover the delaer again!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete!",
            closeOnConfirm: false
        },
        function () {
            window.location.href = currentElement.attr('href');
        }
    );
});

我是这样做的:

<script>

$(document).ready(function () {
    $("#submitDelete").on("click", function () {
        swal({
            title: "Delete important stuff?",
            text: "That seem like your deleting some important Item. Are you sure?",
            dangerMode: true,
            icon: "warning",
            buttons: true,
            dangerMode: true
        }).then((confirmed) => {
            if (confirmed) {
                $(function () {
                    $('#DeleteForm').submit(); // manully submit });
                });
            }
        });
    });
});