sweetAlert preventDefault并返回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; } }); }; }); 

和按钮

  id_dealers}" class="delete" onclick"handleDelete(event);">  //https://stackoverflow.com/questions/30418718/sweetalert-preventdefault-and-return-true/{plink delete! $row->id_dealers} Nette -> calls php delete handler 

我也试过unbind()off()而不是return false ,不起作用。 之前我使用confirm() return true并在onclick属性中return 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

标记看起来像这样

 

而不是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'); } ); });