javascript - SweetAlert2 confirm dialog not working properly in onclick - Stack Overflow

How to fix the sweetalert2 confirm dialog not working properly in onclick event button?I have a modal

How to fix the sweetalert2 confirm dialog not working properly in onclick event button?

I have a modal popup, for CRUD operations before submitting sweetalert2 confirm dialog is triggered.

Here is my modal form code:

<form id="EditForm" asp-action="Edit">
<div class="modal-body">
    
    /*input codes here*/
</div>
<div class="modal-footer justify-content-between">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-success btn-flat" onclick="return submitResult()"><i class="far fa-check-circle"></i> Save Changes</button>
</div>

And here is js code:

function submitResult() {
        Swal.fire({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.isConfirmed) {
                Swal.fire(
                    'Deleted!',
                    'Your file has been deleted.',
                    'success'
                )
                return true;
            }
            else {
                return false;
            }
        })
    }

Sample output

Apply edit, it still does not work

How to fix the sweetalert2 confirm dialog not working properly in onclick event button?

I have a modal popup, for CRUD operations before submitting sweetalert2 confirm dialog is triggered.

Here is my modal form code:

<form id="EditForm" asp-action="Edit">
<div class="modal-body">
    
    /*input codes here*/
</div>
<div class="modal-footer justify-content-between">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-success btn-flat" onclick="return submitResult()"><i class="far fa-check-circle"></i> Save Changes</button>
</div>

And here is js code:

function submitResult() {
        Swal.fire({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.isConfirmed) {
                Swal.fire(
                    'Deleted!',
                    'Your file has been deleted.',
                    'success'
                )
                return true;
            }
            else {
                return false;
            }
        })
    }

Sample output

Apply edit, it still does not work

Share Improve this question edited Nov 17, 2020 at 8:18 Asdf1234567 asked Nov 17, 2020 at 6:25 Asdf1234567Asdf1234567 5602 gold badges14 silver badges31 bronze badges 2
  • 2 Your code is fine. Looks like the page is refreshing because the button click is being treated as a form submission. You'll need to delay form submission until SWAL is plete or disconnect the button from the form. – HymnZzy Commented Nov 17, 2020 at 6:32
  • I think, HymmZyy is right. Just add this code to your submit. e.preventDefault(); – Ultimate Commented Nov 17, 2020 at 6:43
Add a ment  | 

1 Answer 1

Reset to default 4

Try this

<button type="submit" class="btn btn-success btn-flat" onclick="submitResult(event)"><i class="far fa-check-circle"></i> Save Changes</button>

....

function submitResult(e) {
    e.preventDefault();
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            Swal.fire(
                'Deleted!',
                'Your file has been deleted.',
                'success'
            )
            document.getElementById("EditForm").submit();
        }
    })
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744316149a4568191.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信