【问题标题】:jQuery Still Submits Ajax Post Even When "Cancel" is clicked on Confirm Dialog即使在“确认”对话框上单击“取消”,jQuery 仍会提交 Ajax 帖子
【发布时间】:2014-05-27 22:25:52
【问题描述】:

我有以下用于删除用户的脚本。如果我确认要删除用户,该脚本将起作用,但如果我选择取消,该脚本仍会处理 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{
        }
    });

【问题讨论】:

    标签: jquery ajax jquery-ui-dialog confirm


    【解决方案1】:

    in else return 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;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 2023-03-07
      • 1970-01-01
      • 2011-10-17
      相关资源
      最近更新 更多