javascript - 如何在删除前在 toastr 中要求确认
问题描述
我在 PHP LARAVEL 工作,我想要的是一个 toastr 从数据库中删除一个项目。所以当按钮被点击删除时,toastr应该会问“你确定要删除吗?” 在阿贾克斯。如果选择“确定”,则应删除数据,如果选择“取消”,则不应删除数据。如何做到这一点?
下面是我的ajax代码:
//to unfollow feed
$('#following').click(function (e) {
e.preventDefault();
var formData = $("#unfollowFeed").serialize();
$('.error-msg').html('');
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: baseUrl + 'unfollow',
type: 'POST',
data: formData,
success: function (data) {
var id=$('#feed_id').val();
$("#feedId li[feed-id=" + id + "]").remove(); //to remove <li> of removed feed from sidebar.
toastr.error('The feed was removed.');
$('#follow').show();
$('#following').hide();
}
});
});
这是我的观点部分:
<a href style="{{ $status == 0 ? 'display:none;' : '' }}" class=" btn btn-sm btn-primary following" id="following" >{{ __('Following') }}</a>
谁能帮我这个?
解决方案
你可以使用Bootstrap的Sweetalert
$('#following').click(function (e) {
e.preventDefault();
var formData = $("#unfollowFeed").serialize();
$('.error-msg').html('');
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function() {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: baseUrl + 'unfollow',
type: 'POST',
data: formData,
success: function (data) {
var id=$('#feed_id').val();
$("#feedId li[feed-id=" + id + "]").remove(); //to remove <li> of removed feed from sidebar.
toastr.error('The feed was removed.');
$('#follow').show();
$('#following').hide();
}
});
});
});
推荐阅读
- javascript - JavaScript对象的格式化,在产品下显示子产品
- vbscript - vbscript 在不同的浏览器中给出不同的错误
- nightwatch.js - 浏览器一出现 Nightwatch 就会崩溃 - 正在工作并且没有进行任何更改
- node.js - 节点和 SQL Server 批量插入的 ERR_OUT_OF_RANGE 问题
- flask-login - 烧瓶登录当前用户在页面刷新/重定向时消失
- php - mariadb, PHP7 -- 不能授予所有数据库权限
- sql-server - 试图将熊猫数据框插入临时表
- visual-studio-code - 在 vscode 选项卡中显示更多路径
- laravel - 从树枝中的数据库动态显示背景图像
- jquery - 如何做 php jquery load_more 数据