jQuery仍然提交Ajax发布即使"取消"单击确认对话框

时间:2014-04-14 04:32:02

标签: jquery ajax jquery-ui-dialog confirm

我有以下用于删除用户的脚本。如果我确认我要删除用户但是如果我选择取消,该脚本仍然有效,脚本仍会处理ajax提交并删除用户。我认为通过设置一个空的else {}语句将作为"什么都不做"但显然那里我错了。

        $("#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{
        }
    });

1 个答案:

答案 0 :(得分:2)

in else 返回false ,如下所示:

$("#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;
    }
});