首页 > 解决方案 > 如何在删除前在 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>

谁能帮我这个?

标签: javascriptphpjqueryajaxlaravel

解决方案


你可以使用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();

                }

            });
        });
    });

推荐阅读