设置showCancelButton为false后SweetAlert2仍显示取消按钮问题
我在使用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
相关产品推荐
相关产品推荐

