2

我的 SweetAlert 确认没有等待任何操作。确认框闪烁,然后将执行操作。怎么了?

<script type="text/javascript">
    function confirmDelete() {
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Delete",
            cancelButtonText: "Cancel",
            closeOnConfirm: false,
            closeOnCancel: false
        });
    }
</script>

<form onsubmit="confirmDelete()" action="index.php" method="post">
    <button type="submit" name="delete">Delete</button>
</form>

这个也试过了。同样的问题...窗口只是闪烁并执行操作。

<script type="text/javascript">
    function confirmDelete() {
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Delete",
            cancelButtonText: "Cancel",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function(isConfirm) {
            if (isConfirm) {
                document.deleteForm.submit();
            }
        });
    }
</script>

<form id="deleteForm" action="index.php" method="post">
    <button onclick="confirmDelete()" type="input" name="delete">Delete</button>
</form>
4

2 回答 2

0

按以下方式更改您的代码:

鉴于:

<form id="my_form" action="index.php" method="post">
     <a href="javascript:void(0);" class="btn btn-primary _delete_data">DELETE</a>
</form>

在js代码中:

<script>
    $(document).ready(function(){
        $('._delete_data').click(function(e){

            Swal.fire({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {

                if (result.value) {
                    $(document).find('#my_form').submit();
                }
            })
        });
    });            
</script>
于 2019-05-27T04:12:01.020 回答
0

您可以使用 preventDefault:

<form id="deleteForm" action="index.php" method="post">
    <button id="delete-btn" type="input" name="delete">Delete</button>
</form>

<script type="text/javascript">
    $("#delete-btn").on("click", function(e) {
        e.preventDefault(); //prevents the form from being submitted
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Delete",
            cancelButtonText: "Cancel",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function(isConfirm) {
            if (isConfirm) {
                document.deleteForm.submit();
            }
        });
    }
</script>
于 2018-06-22T11:45:15.673 回答