javascript - SweetAlert2 确认对话框在 onclick 中无法正常工作
问题描述
如何修复事件按钮sweetalert2
中的确认对话框无法正常工作?onclick
我有一个模式弹出窗口,用于在提交 sweetalert2 确认对话框之前进行 CRUD 操作。
这是我的模态表单代码:
<form id="EditForm" asp-action="Edit">
<div class="modal-body">
/*input codes here*/
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success btn-flat" onclick="return submitResult()"><i class="far fa-check-circle"></i> Save Changes</button>
</div>
这是js代码:
function submitResult() {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
return true;
}
else {
return false;
}
})
}
解决方案
试试这个
<button type="submit" class="btn btn-success btn-flat" onclick="submitResult(event)"><i class="far fa-check-circle"></i> Save Changes</button>
....
function submitResult(e) {
e.preventDefault();
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
document.getElementById("EditForm").submit();
}
})
}
推荐阅读
- dax - 获取列的最后一个非空白值
- mongodb - Scala MongoDB groupby 多个字段
- powershell - 如何使用 powershell 连接到 Microsoft Teams
- php - 删除 altervista 的 .htaccess 后,该站点不断将我重定向到旧 URL
- html - 页眉与打印预览模式中的内容重叠
- 3d - Gnuplot:来自部分参数化输入的 3D 图
- reactjs - 酶测试错误,方法“模拟”意味着在 1 个节点上运行。找到了 0 个
- android - 在宽松的内存模型下触发错误(尤其是部分存储排序)
- c - 为什么在函数的链表头部添加新节点时需要使用指向指针的指针?
- c# - C# - 如何将表单数据发布到 PHP 文件并将用户永久重定向到 url?