在按OK之前,Onclick删除不会停止 [英] Onclick Delete not stopping before pressing Okay

查看:52
本文介绍了在按OK之前,Onclick删除不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用onclick进行删除通话

I am using an onclick on a delete call

"<a href='" + Url.Action("Delete", "OBProfile", new { id = "#= ProfileID#" })
 + "' title='Delete' id='deleteButton' class='btn btn-danger btn-sm 
deleteButton' onclick='return confirm_delete(#= ProfileID#)'>Delete</a>"

这是它运行的脚本

function confirm_delete() {
        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 () {
            swal("Deleted!", "Your imaginary file has been deleted!", "success");
        });
    };

问题在于它不会停止并等待按下Delete键.它调用脚本,弹出框,删除记录并刷新页面.单击确认按钮时,需要它进行处理.这是通过 SweetAlert

The issue is that it doesn't stop and wait for the Delete button to be pressed. it calls the script, the box pops up, it deletes the record, and refreshes the page. I need it to process when I click the confirm button. This is done using SweetAlert

推荐答案

因此,无论如何,您都必须使用jQuery来使swal正常工作.因此,请勿像使用(onclick='return confirm_delete...)那样使用内联事件处理程序.使用事件绑定jQuery方式,防止默认行为,在确认删除后,如下所示设置要转到链接URL的位置.

So, you anyway have to use jQuery for the swal to work. Therefore, do not use inline event handler like you did (onclick='return confirm_delete...). Use event binding the jQuery way, prevent the default behaviour, when delete is confirmed, set the location to go to the link URL as below.

$("#deleteButton").on("click", function(evt) {
    evt.preventDefault();
    var _this = this;
    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(isConfirm) {
        if (isConfirm) {
            swal("Deleted!", "Your imaginary file has been deleted.", "success");
            location.href = _this.href;
        }
    });
});

<link href="https://t4t5.github.io/sweetalert/dist/sweetalert.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://t4t5.github.io/sweetalert/dist/sweetalert-dev.js"></script>
<a href='http://www.aol.com' title='Delete' id='deleteButton' class='btn btn-danger btn-sm 
deleteButton'>Delete</a>

这篇关于在按OK之前,Onclick删除不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆