You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jQuery SweetAlert插件遇"Unexpected 2nd argument"错误求助

解决SweetAlert的"Unexpected 2nd argument"错误

嘿,我一眼就看出问题出在哪啦!你遇到的这个Uncaught SweetAlert: Unexpected 2nd argument错误,核心原因是SweetAlert版本的语法差异——你用的应该是新版的SweetAlert2,但写法还停留在旧版SweetAlert1.x的逻辑上。

问题根源

旧版SweetAlert(1.x)允许把回调函数直接作为swal()的第二个参数,但新版SweetAlert2已经改用了Promise风格的API:所有弹窗配置都要放在第一个对象参数里,点击确认/取消后的逻辑必须通过.then()方法来处理,不能再把回调当第二个参数传进去,这就是为啥会报“意外的第二个参数”错误。

另外看你代码片段里的showCancelButto...,大概率是拼写漏了个n(应该是showCancelButton),这也可能导致配置不生效哦。

修改后的正确代码

下面是适配SweetAlert2的修正版代码,我帮你调整了语法和配置项:

$('.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.",
    icon: "warning", // SweetAlert2里用icon替代了旧版的type
    showCancelButton: true, // 补全拼写
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: "Cancel"
  }).then((result) => {
    // 处理确认后的逻辑
    if (result.isConfirmed) {
      // 这里放你原来setTimeout里的代码
      setTimeout(function() {
        console.log("开始删除帖子ID:", postID);
        // 比如发起AJAX删除请求或者执行DOM操作
      }, 1000);
    }
  });
});

额外注意点

  1. 如果你坚持用SweetAlert1.x,那要确保配置项拼写正确(比如把showCancelButto改成showCancelButton),但更推荐升级到SweetAlert2——旧版已经停止维护了,新版功能更全、兼容性更好。
  2. 检查你的SweetAlert引入是否对应版本:SweetAlert2的引入链接通常是https://cdn.jsdelivr.net/npm/sweetalert2@11,别和旧版混着用。

内容的提问来源于stack exchange,提问作者sach jot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:34:13