如何运行SweetAlert而不是默认的javascript确认方法 [英] How to run a SweetAlert instead of default javascript confirm method

查看:92
本文介绍了如何运行SweetAlert而不是默认的javascript确认方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前这是我用来运行基于确认类的正常确认窗口的代码。这一切都是通过href链接完成的,而不是onClick事件按钮。由于点击的结果是运行另一个被剪切的代码放在另一个文件中(意图删除数据库中的一行)。

Currently this is the code I use to run a normal confirm window based on the class "confirmation". This is all done with an href link and not on a button onClick event. As the result of the click is to run another code snipped placed in a different file (with the intention to delete a row in db).

$('.confirmation').on('click', function () {
        return confirm('Er du sikker på at du vil slette?');
    });

我想要的是用这个SweetAlert函数替换confirm方法

What I want is to replace the confirm method with this SweetAlert 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"); 
});

任何人都知道如何做到这一点,当我发生什么事时尝试将sweetalert放在onClick函数中是警报出现但它会自动删除行而不必我确认任何内容并且警报会淡出。

Anyone know how to do this, what happens when I try to place the sweetalert inside the onClick function is that the alert appears but it automatically delete the row without me having to confirm anything and the alert fades out.

推荐答案

我找到了解决方案!

$('.confirmation').click(function(e) {
e.preventDefault(); // Prevent the href from redirecting directly
var linkURL = $(this).attr("href");
warnBeforeRedirect(linkURL);
});

 function warnBeforeRedirect(linkURL) {
    swal({
      title: "Leave this site?", 
      text: "If you click 'OK', you will be redirected to " + linkURL, 
      type: "warning",
      showCancelButton: true
    }, function() {
      // Redirect the user
      window.location.href = linkURL;
    });
  }

这篇关于如何运行SweetAlert而不是默认的javascript确认方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆