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

设置showCancelButton为false后SweetAlert2仍显示取消按钮问题

SweetAlert2设置showCancelButton:false仍显示取消按钮的问题排查

我在使用SweetAlert2弹窗组件时,已将配置项showCancelButton设置为false,但弹窗仍显示取消按钮。有没有其他开发者遇到过该问题?

示例代码

Swal.fire({
    title: _title,
    text: _desc,
    icon: 'warning',
    showCancelButton: false,
    confirmButtonColor: '#3085d6',
    confirmButtonText: 'Yes, proceed!'
}).then((result) => {
    if (result.value) {
        isLoading(1);
        $.get("ajax/asset/sunset_asset.aspx", { id: assetId, status: assetNewStatus }, function (data) {
            if (data == "OK") {
                toastr.success(_responseOK, 'Success');
                refreshResults();
            } else {
                toastr.error(data, 'Error');
            }
            isLoading(0);
        });
    }
});

问题截图

弹窗显示取消按钮截图

可能的解决方向

  • 检查全局默认配置:如果项目中之前调用过Swal.setDefaults({ showCancelButton: true }),全局配置会覆盖单个弹窗的设置。可以在弹窗代码前执行console.log(Swal.getDefaults())确认默认值。
  • 升级SweetAlert2版本:部分旧版本存在该bug,尝试升级到最新稳定版,执行命令:npm install sweetalert2@latest(或对应包管理工具的更新命令)。
  • 排查自定义/第三方代码干扰:项目中可能存在封装SweetAlert2的自定义函数,或者第三方插件强制修改了弹窗配置,检查是否有全局拦截、修改Swal参数的代码。
  • 清理浏览器缓存:确保加载的是最新的SweetAlert2脚本文件,避免旧代码缓存导致的异常。

内容的提问来源于stack exchange,提问作者Robert Benedetto

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 02:32:41