【问题标题】:Sweet Alert confirm with Node JS and ExpressSweet Alert 通过 Node JS 和 Express 确认
【发布时间】:2020-02-23 05:02:05
【问题描述】:

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

但是,我想确保“甜蜜警报”确认消息,我正在努力解决这个问题。

【问题讨论】:

    标签: node.js express ejs sweetalert sweetalert2


    【解决方案1】:

    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
          }
     })); };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多