javascript - 删除表中的行时如何使用sweetalert
问题描述
我想在链接我的控制器之前删除表中的数据时使用sweetalert。但它只调用我的控制器并且不显示任何警报。
<a href="Table/Delete/<?php echo ($u->ID) ?>">
<button id="a" type="button" class="btn btn-danger"> <i class="glyphicon glyphicon-trash"></i> Delete </button>
</a>
js
<script>
$('#a').on('click',function(event){
event.preventDefault();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal("Poof! Your imaginary file has been deleted!", {
icon: "success",
});
} else {
swal("Your imaginary file is safe!");
}
});
})
</script>
解决方案
不要使用<a href="...">
链接
HTML 应该像 ..
<div>
<button id="a"></button>
</div>
js应该如下
<script>
$('#a').on('click',function(event){
event.preventDefault();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
# delete row
# show swal and redirect to your link
} else {
swal("Your imaginary file is safe!");
}
});
})
</script>
推荐阅读
- java - 在子类的构造函数中抛出异常
- node.js - 如何使用cheerio循环结果
- http-live-streaming - 将 AAC 的 avcodec 配置配置为 HLS/MPEG-TS([mpegts @ 0x7fc4c00343c0] AAC 比特流不是 ADTS 格式并且缺少额外数据)
- java - 无法将 JSON 字符串转换为对象?
- javascript - Sequelize 和 Postgres - 无法实现外键约束
- c# - 如何在 Unity 3D 上正确配置触摸?
- python - 如何在循环中创建模型字段?
- reactjs - 如何在 Reactjs 中使用路由器包装多个上下文提供程序
- javascript - 错误:重新渲染过多。React 限制渲染次数
- c# - 删除地标并将减去地标的整个 kml 保存到新文件中