用户确认动作后如何执行脚本?

时间:2019-07-18 21:06:39

标签: jquery ajax asp.net-mvc sweetalert2

我正在使用SweetAlert库删除记录,我希望当用户单击删除按钮时出现警告消息。当我按下删除按钮时,出现消息,然后执行删除,我想避免这种情况,我只是希望在用户确认后执行删除。

我使用Jquery和Ajax进行了此操作::

<script>
$(document).ready(function () {
    $(".btn-eliminar").on("click", function () {
        Swal.fire({
            title: 'Are you sure you want to delete this Chat?',
            text: "You will not be able to recover the data!",
            type: 'warning',
            showCloseButton: true,
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!',
            cancelButtonText: 'Cancel'
        }).then(function (isConfirm) {
            if (isConfirm) {
                Swal.fire(
                    'Removed!',
                    'A chat was deleted.',
                    'success'
                ).then(function () {

                        $.ajax({
                                type: "POST",
                                url: '@Url.Action("DeleteChat", "Chat")',
                                data: { id: $(this).parent().siblings(".td-id").text().trim() },
                                success: function (rpta) {

                                },
                                error: function (req, textStatus, errorThrown) {
                                    alert('Ooops, something happened: ' + textStatus + ' ' + errorThrown);
                                }
                           });

                    });
            } else {

            }
            });
    });
});

1 个答案:

答案 0 :(得分:1)

尝试一下:

$(document).ready(function() {
    $(".btn-eliminar").on("click", function() {
        Swal.fire({
            title: 'Are you sure you want to delete this Chat?',
            text: "You will not be able to recover the data!",
            type: 'warning',
            showCloseButton: true,
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!',
            cancelButtonText: 'Cancel'
        }).then((isConfirm) => {
            if (isConfirm.value) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("DeleteChat", "Chat")',
                    data: {
                        id: $(this).parent().siblings(".td-id").text().trim()
                    },
                    success: function(rpta) {

                    },
                    error: function(req, textStatus, errorThrown) {
                        alert('Ooops, something happened: ' + textStatus + ' ' + errorThrown);
                    }
                });

            } else {

            }
        });
    });
});