即使“取消",jQuery仍会提交Ajax发布.在确认对话框上单击 [英] jQuery Still Submits Ajax Post Even When "Cancel" is clicked on Confirm Dialog

查看:84
本文介绍了即使“取消",jQuery仍会提交Ajax发布.在确认对话框上单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用于删除用户的脚本.如果我确认要删除用户,则脚本可以工作,但是如果我选择取消,则脚本仍会处理ajax提交并删除用户.我以为通过设置一个空的else {}语句将充当不执行任何操作",但是显然我在那是错的.

I have the following script I use for deleting users. The script works if I confirm that I want to delete the user but if I choose to cancel, the script still processes the ajax submission and deletes the user. I thought by setting an empty else{} statement would act as "do nothing" but apparently I was wrong there.

        $("#user_delete_submit").click(function(){
        var dataString = $("#frm_user_delete").serialize();
        if(confirm("This cannot be undone, are you sure?")){
            $.ajax({
                type: "POST",
                url: "/admin/user_delete.php",
                data: dataString,
                dataType : "json"
            })
            .done(function (data) {
                $("#user_delete_dialog").dialog({
                    autoOpen: false,
                    modal: true,
                    close: function(event, ui) { window.location.href = "/admin/user_list.php"; },
                    title: "Account Deletion",
                    resizable: false,
                    width: 500,
                    height: "auto"
                });
                $("#user_delete_dialog").html(data.message);
                $("#user_delete_dialog").dialog("open");
            });
            return false; // required to block normal submit since you used ajax
        }else{
        }
    });

推荐答案

在其他情况下返回false ,如下所示:

in else return false like this:

$("#user_delete_submit").click(function(){
    var dataString = $("#frm_user_delete").serialize();
    if(confirm("This cannot be undone, are you sure?")){
        $.ajax({
            type: "POST",
            url: "/admin/user_delete.php",
            data: dataString,
            dataType : "json"
        })
        .done(function (data) {
            $("#user_delete_dialog").dialog({
                autoOpen: false,
                modal: true,
                close: function(event, ui) { window.location.href = "/admin/user_list.php"; },
                title: "Account Deletion",
                resizable: false,
                width: 500,
                height: "auto"
            });
            $("#user_delete_dialog").html(data.message);
            $("#user_delete_dialog").dialog("open");
        });
        return false; // required to block normal submit since you used ajax
    }else{
       return false;
    }
});

这篇关于即使“取消",jQuery仍会提交Ajax发布.在确认对话框上单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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