jQuery Sweet Alert意外的第二个参数错误

时间:2018-04-20 22:52:56

标签: javascript jquery sweetalert

我正在使用甜蜜警报插件并收到错误消息。我已尝试过每个例子,但无法理解这个错误意味着什么

  

Uncaught SweetAlert:意外的第二个参数(function(){    setTimeout(function(){ckquote

我的代码:

<script type="text/javascript">
$('.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) {
                    swal({
                            title: "Deleted!",
                            text: "Your post has been deleted.",
                            type: "success"
                        },
                    );
                }
            );

        }, 50);
    });
});
</script>

我在控制台窗口上的整个错误:

sweetalert.min.js:1 Uncaught SweetAlert: Unexpected 2nd argument (function() {
 setTimeout(function() {
 $.post("../delete.php", {
 id: postID
 },
 function(data) {
 swal({
 title: "Deleted!",
 text: "Your post has been deleted.",
 type: "success"
 },
 );
 }
 );

 }, 50);
 })

1 个答案:

答案 0 :(得分:3)

Sweet Alert可以通过两种方式调用

  • 1,2或3个字符串参数

    swal(["title",] "text" [, "iconname"])
    
  • 包含所有选项的单个对象参数:

    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!",
    });
    

如果您想对响应执行某些操作,则会返回一个承诺,您可以使用.then检索该值:

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!",
}).then(function() {
  setTimeout(function() {
    $.post("../delete.php", {
        id: postID
      },
      function(data) {
        swal({
          title: "Deleted!",
          text: "Your post has been deleted.",
          type: "success"
        }, );
      }
    );

  }, 50);
});