【问题标题】:SweetAlert confirm with Ajax requestSweetAlert 通过 Ajax 请求确认
【发布时间】:2015-10-08 05:48:08
【问题描述】:

我是 Javascript 的新手——实际上是第一次编写它。 我正在尝试使用SweetAlert 进行删除确认按钮。当我按下带有onclick="confirmDelete()" 的按钮时,什么也没有发生。这段代码可能只是螃蟹,但在这里:

<script type="text/javascript">
    function confirmDelete() {
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: false
        )},
            $.ajax({
                url: "scriptDelete.php",
                type: "POST",
                data: {id: 5},
                dataType: "html",
                success: function () {
                    swal("Done!","It was succesfully deleted!","success");
                }
            });
    }
</script>

<a href="#" onclick="confirmDelete()">Delete</a>

如果删除失败,我可以添加任何警报吗?

【问题讨论】:

  • 也请添加相关的html代码
  • @DhirajBodicherla,我编辑了帖子并添加了&lt;a&gt;&lt;/a&gt;...或者你的意思是什么?

标签: javascript jquery ajax sweetalert


【解决方案1】:

试试这个代码。它对我来说很好。

$('.delete-confirm').on('click', function() {
    var postID = $(this).val();
    console.log(postID);
    swal({
        title: "Are you sure?",
        text: "If you delete this post all associated comments also deleted permanently.",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        showLoaderOnConfirm: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes, delete it!",
    }, function() {
        setTimeout(function() {
            $.post("delete.php", {
                    id: postID
                },
                function(data, status) {
                    swal({
                            title: "Deleted!",
                            text: "Your post has been deleted.",
                            type: "success"
                        },
                        function() {
                            location.reload();
                        }
                    );
                }
            );

        }, 50);
    });
});

【讨论】:

    【解决方案2】:

    3 年后,我终于可以为那个家伙工作了。

    function dropConfig(config_index){
    
      swal({
       title: "WARNING:", 
       text: "Are you sure you want to delete this connection?", 
       type: "warning",
       inputType: "submit",
       showCancelButton: true,
       closeOnConfirm: true,
       timer: 2000
           }, //end swal   }
    
    
    function(isConfirm) {
          if (isConfirm == true) {
    
             //do the ajax stuff.
             $.ajax({
                method: "POST",
                url: "/drop_config",
                data: {"curr_config":  $("#curr_conf_conn_name_" + config_index).val()}})
               .success(function(msg) {
               show_notification(msg,"success");
               setInterval(function() {.reload();
               }, 2500);})
               .error(function(msg) {show_notification(msg.responseText,"danger");});
    
    
    
    
          } // end if }
       }); //  end function } & end swal )
    }     //   end function }
    

    【讨论】:

      【解决方案3】:

      如果我正确理解您的问题,您是在询问如何处理 ajax 请求中的错误情况。 Ajax设置有error属性,可以这样使用

      $.ajax({
        .... other settings you already have
        error: function (xhr, ajaxOptions, thrownError) {
          swal("Error deleting!", "Please try again", "error");
        }
      });
      

      另外,您以错误的方式调用 swal。 Swal 有这样的回调

      swal({settings}, function(isConfirm){});
      

      整体代码如下所示

      function confirmDelete() {
          swal({
              title: "Are you sure?",
              text: "You will not be able to recover this imaginary file!",
              type: "warning",
              showCancelButton: true,
              confirmButtonColor: "#DD6B55",
              confirmButtonText: "Yes, delete it!",
              closeOnConfirm: false
          }, function (isConfirm) {
              if (!isConfirm) return;
              $.ajax({
                  url: "scriptDelete.php",
                  type: "POST",
                  data: {
                      id: 5
                  },
                  dataType: "html",
                  success: function () {
                      swal("Done!", "It was succesfully deleted!", "success");
                  },
                  error: function (xhr, ajaxOptions, thrownError) {
                      swal("Error deleting!", "Please try again", "error");
                  }
              });
          });
      }
      

      这是一个演示http://jsfiddle.net/dhirajbodicherla/xe096w10/33/

      【讨论】:

        【解决方案4】:

        您在swal({)} 中有错误,应该是swal({})

        更新代码:

        <script type="text/javascript">
            function confirmDelete() {
                swal({
                    title: "Are you sure?",
                    text: "You will not be able to recover this imaginary file!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Yes, delete it!",
                    closeOnConfirm: false
                },
                 function(isConfirm){
                   if (isConfirm) {
                    $.ajax({
                        url: "scriptDelete.php",
                        type: "POST",
                        data: {id: 5},
                        dataType: "html",
                        success: function () {
                            swal("Done!","It was succesfully deleted!","success");
                        }
                    });
                  }else{
                        swal("Cancelled", "Your imaginary file is safe :)", "error");
                  } 
               })
            }
        </script>
        

        【讨论】:

        • 感谢您的回答。我试过这段代码,但它不能正常工作。如果我按取消,则不会出现任何通知。以及如果 scriptDelete.php 出现错误,我将如何收到错误通知...
        • 我也在我的代码中加入了条件......你也检查过吗
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-26
        • 1970-01-01
        • 1970-01-01
        • 2014-06-07
        • 2018-06-21
        相关资源
        最近更新 更多