【问题标题】:Implementing confirm() with Jquery Sweet Alert使用 Jquery Sweet Alert 实现 confirm()
【发布时间】:2015-10-12 13:57:31
【问题描述】:

我想用著名的甜蜜警报插件在 JavaScript 上实现confirm()。但不知何故,甜蜜的警报不起作用。

这是我的代码:

function kendaraan(param) {
        if (swal({
                title: "Are you sure?",
                text: "You wanna leave this page?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, Leave!",
                closeOnConfirm: false
              },
                function(){

                })
            ){
            switch (param) {
                case "ADD_KENDARAAN" :
                    $.ajax({
                        type: "POST",
                        url: "pages/lj_menu/vehicle/add_vehicle.php",
                        success: function (response, textStatus, jqXHR) {
                            $('#lj-mainpage').html(response);
                        }
                    });
                    break;
            }
        }
    }

所以我的意图是当用户单击“添加 KENDARAAN”时,它应该显示甜蜜的警报确认,当用户单击是时,然后执行 ajax 部分。

【问题讨论】:

    标签: javascript php jquery ajax sweetalert


    【解决方案1】:

    不要使用if,因为它没有必要。根据文档,只需在anonymous function 中为success 实现您的switch,如下所示:

    function kendaraan(param) {
         swal({   
               title: "Are you sure?",
               text: "You wanna leave this page?",
               type: "warning",
               showCancelButton: true,
               confirmButtonColor: "#DD6B55",
               confirmButtonText: "Yes, Leave!",
               closeOnConfirm: false
         },
         function(){   
               switch (param) {
                    case "ADD_KENDARAAN" :
                          $.ajax({
                                type: "POST",
                                url: "pages/lj_menu/vehicle/add_vehicle.php",
                                success: function (response, textStatus, jqXHR) 
                                         {
                                            $('#lj-mainpage').html(response); 
                                            swal("Deleted!", "Your imaginary file has been deleted.", "success"); });
                                            //keep success message here if you wish to
                                 }
                          });
                      break;
                }
         });
    }
    

    From the DOCS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2018-07-09
      相关资源
      最近更新 更多