【问题标题】:How to get a script executed when the user confirms an action?当用户确认操作时如何执行脚本?
【发布时间】:2019-07-18 21:06:39
【问题描述】:

我正在使用 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 {

            }
            });
    });
});

【问题讨论】:

    标签: jquery ajax asp.net-mvc sweetalert2


    【解决方案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 {
    
                }
            });
        });
    });
    

    【讨论】:

    • 你好@FarhaniWalid,你的回答我没有成功,我检查了 SweetAlert 的文档,我也没有成功。
    • 警报出现,然后我的记录被删除。它不允许我显示警报,因此用户可以阅读消息。
    • 您好,请您逐步向我解释您想做什么?
    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多