jquery模态确认问题

时间:2013-07-04 17:28:56

标签: php jquery modal-dialog

我有一个删除输入按钮$_POST到同一页面。我有一个jquery确认模式,点击它时弹出。如果是,那么它会发布表单,如果没有则取消该操作。

<?php
    if ($_POST['doDelete'] == 'Delete') {
        if (!empty($_POST['u'])) {
            foreach ($_POST['u'] as $uid) {
                $id = filter($uid);
                if (deleteRecord('projects', $id, false)) {
                    $fail[] = $id;
                } else {
                    $pass[] = $id;
                }
            }
        }
        $msg = new Messages();
        if (!empty($pass)) {
            $passed = implode(', ', $pass);
            $message = "Deleted: {$passed}";
            $msg->add('s', $message);
        }
        if(!empty($fail)){
            $failure = implode(', ', $fail);
            $message = "Could not delete/an error occured: {$failure}";
            $msg->add('e', $message);
        }
        redirect('projects.php');
    }
?>
<script>
    $(function () {
        $("#dialog-confirm-multiple").dialog({
            autoOpen: false,
            resizable: false,
            width: 300,
            modal: true,
            show: {
                effect: "bounce",
                duration: 100
            },
            hide: "drop",
            buttons: {
                "Yes": function () {
                    $("#confirm").submit();
                },
                "No": function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#doDelete").click(function (e) {
            e.preventDefault();
            $("#dialog-confirm-multiple").dialog('open');
            return false;
        });
    });
</script>
<form post="self.php" id="confirm">
<!-- some inputs .etc -->
<input name="doDelete" type="submit" id="doDelete" value="Delete" class="btn btn-danger">
</form>

按钮ID为doDelete,表单ID为confirm。但是,模式仅在用户单击yes而不发布表单后自行关闭。当我注释掉这段代码时,按钮工作正常,并删除记录(帖子成功)。

类似于:jquery dialog: confirm the click on a submit button

2 个答案:

答案 0 :(得分:0)

亚历:

尝试按以下方式进行ajax调用:

$.ajax({
        type: "POST",
        url: "YOUR_PHP_URL",
        data: "YOUR_VARIABLE",
        async: false,
        beforeSend: function () { /// use beforeSend to open a dialog box 

        $("#dialog-confirm-multiple").dialog({
          //...

        });


        },
        success: function (response) {
            //the dialog will just load but the form will still continue to submit                             
        }

    });

当您使用“beforeSend”功能时,魔法就会出现......我希望我可以帮助您...

答案 1 :(得分:0)

您是否尝试在关闭对话框之前发送表单?

$("#dialog-confirm-multiple").dialog({
    buttons: {
        "Yes": function () {
            $("#confirm").submit();
            $(this).dialog("close");
        },
        "No": function () {
            $(this).dialog("close");
        }
    }
});