确认暂停表单提交,直到按下确定 [英] Confirmation to pause form submit until Ok has beenpushed

查看:102
本文介绍了确认暂停表单提交,直到按下确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体,现在会弹出一个确认对话框(是否要继续),但是该窗体将同时发送到我的控制器,无论您单击是还是否,该窗体都会开始处理.如何暂停表单提交,直到显示/选择javascript确认为止.

I have a form that a confirmation dialog pops up (do you want to continue) right now it pops up but the form is being sent to my controller at the same time, which begins processing regardless if you hit yes or no. How do I pause the form submit until after the javascript confirmation has been shown/selected.

 @using (Html.BeginForm("Process", "Home", FormMethod.Post, 
 new { id ="MyForm" })) {  
    @Html.TextBoxFor(m => m.Name); 
    // other form controls...
    <input id="Submit" type="submit" value="Apply Changes" /> 
 }

javascript :(使用jquery-ui)

javascript: (using jquery-ui)

 $(function () {
        $("#Confirm").dialog({
            autoOpen: false,
            buttons: {
                "Ok": function() {
                    $(this).dialog("close");
                    $('#MyForm').submit();
                    return true;
                },
                Cancel: function() {
                    $(this).dialog("close");
                    event.preventDefault();
                    return false;
                }
            }
        });

        $("#Submit").click(function () {
            $("#Confirm").dialog("open");
        });
    });

推荐答案

立即阻止默认操作,然后按OK按钮触发提交.

Prevent the default action immediately, then trigger the submit on the ok button press.

 $(function () {
    $("#Confirm").dialog({
        autoOpen: false,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
                $('#MyForm')[0].submit();
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });

    $("#MyForm").submit(function (e) {
        e.preventDefault();
        $("#Confirm").dialog("open");
    });
});

这篇关于确认暂停表单提交,直到按下确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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