使用Node JS和Express确认甜蜜警报 [英] Sweet Alert confirm with Node JS and Express

查看:17
本文介绍了使用Node JS和Express确认甜蜜警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用.EJS模板的Node JS/Express应用程序,在编辑或删除记录之前,我使用以下代码弹出标准的"你确定吗"消息。这两个都工作正常:

<a id="edit-form" href="/blog/<%-blog._id%>/edit" onclick="return confirm('Are you sure you wish to edit this blog post?');">
    <div class="new-show-icons"><i class="far fa-edit"></i></div>
</a>
<form id="btn-form" action="/blog/<%- blog._id %>?_method=DELETE" method="POST" onclick="return confirm('Are you sure you wish to delete this blog post?');">
    <div class="new-show-icons"><i class="far fa-trash-alt"></i></div>
</form>
这里是我到目前为止拥有的弹出窗口的JS代码,但我不确定1)如何替换这些弹出窗口以代替标准的html确认消息,以及2)如何指定在用户确认或取消时应该采取什么操作(如果是编辑,请将它们带到编辑页面,如果是删除,则直接删除记录)。如有任何帮助,我们将不胜感激。谢谢

function areYouSureEdit() {
    swal({
        title: "Are you sure you wish to edit this record?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: '#DD6B55',
        confirmButtonText: 'Yes!',
        closeOnConfirm: false,
    },
    function(){

    });
};

function areYouSureDelete() {
    swal({
        title: "Are you sure you wish to delete this record?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: '#DD6B55',
        confirmButtonText: 'Yes, delete it!',
        closeOnConfirm: false,
    },
    function(){
        swal("Deleted!", "Your imaginary file has been deleted!", "success");
    });
};
但是,我想确保‘甜蜜警报’确认消息,这是我正在努力解决的问题。

推荐答案

SweetAlert使用Promises跟踪用户与警报的交互方式。

如果用户单击确认按钮,承诺将解析为true。如果解除警报(通过在警报之外单击),则承诺解析为空。(ref) 因此,由于有指南

function areYouSureEdit() {
swal({
    title: "Are you sure you wish to edit this record?",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: '#DD6B55',
    confirmButtonText: 'Yes!',
    closeOnConfirm: false,
}.then((value) => {
  if(value){
           //bring edit page
     }else{
       //write what you want to do
      }
 }) };

function areYouSureDelete() {
  swal({
    title: "Are you sure you wish to delete this record?",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: '#DD6B55',
    confirmButtonText: 'Yes, delete it!',
    closeOnConfirm: false,
}.then((value) => {
  if(value){
           //ajax call or other action to delete the blog
        swal("Deleted!", "Your imaginary file has been deleted!", "success");
     }else{
       //write what you want to do
      }
 })); };

这篇关于使用Node JS和Express确认甜蜜警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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