通过节点JS和Express进行的甜警报确认

问题描述 投票:0回答:1

我有一个使用.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");
    });
};

但是,我要确保正在努力处理的“ Sweet Alert”确认消息。

node.js express ejs sweetalert sweetalert2
1个回答
0
投票

SweetAlert使用promise来跟踪用户如何与警报交互。

如果用户单击确认按钮,则诺言将解析为true。如果警报被消除(通过在警报外部单击),则承诺解析为null。 (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
      }
 })); };
© www.soinside.com 2019 - 2024. All rights reserved.