0

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

这是我的代码:

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 部分。

4

1 回答 1

0

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

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

于 2015-10-12T14:13:33.117 回答