jQuery 模式对话框未提交我的表单

发布于 2024-10-13 02:47:38 字数 1263 浏览 4 评论 0原文

我正在使用 jQuery 模态对话框询问用户是否希望提交表单。

但是,在用户单击对话框的“提交”按钮后,表单并未提交。

如果我再次单击表单提交按钮,它就会提交。

我猜这是一个范围问题,我看过其他一些关于它的帖子,但到目前为止已经花了很多时间但没有运气。关于如何解决这个问题有什么想法吗?

JavaScript

$(document).ready(function(){
    var submitForm = $('#myform');
    submit = false;

    $("#confirm").dialog({
        resizable: false,
        height: 140,
        modal: true,
        autoOpen: false,
        buttons: {
            'Submit': function() {
                $(this).dialog('close');
                submit = true;
                submitForm.submit();
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#confirm").parent().appendTo($("#myform")); 

    submitForm.submit(function() {
        if (submit) {
            return true;
        } else {
            $("#confirm").dialog('open');
            return false;
        }
    });
});

HTML

<form id="myform" action="#" method="post">
    <input type="text" name="check_me" />
    <input type="submit" name="submit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>

I am using a jQuery Modal Dialog box to ask the user if they wish to submit the form or not.

However after the user clicks Dialog's Submit button, the form is not submitting.

If I then click the forms submit button again, it submits.

I am guessing this is a scope issue, I have seen some other posts about it but as yet have spent many hours with no luck. Any ideas on how to solve this?

Javascript

$(document).ready(function(){
    var submitForm = $('#myform');
    submit = false;

    $("#confirm").dialog({
        resizable: false,
        height: 140,
        modal: true,
        autoOpen: false,
        buttons: {
            'Submit': function() {
                $(this).dialog('close');
                submit = true;
                submitForm.submit();
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#confirm").parent().appendTo($("#myform")); 

    submitForm.submit(function() {
        if (submit) {
            return true;
        } else {
            $("#confirm").dialog('open');
            return false;
        }
    });
});

HTML

<form id="myform" action="#" method="post">
    <input type="text" name="check_me" />
    <input type="submit" name="submit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哽咽笑 2024-10-20 02:47:38

解决方案很简单...您的按钮名称。你为什么问?我会告诉你!

拿这个 jsfiddle 并注意我只做了一些更改。 javascript 没有改变,只是 HTML

HTML

<form id="myform" action="javascript:alert('Success!')" method="post">
    <input type="text" name="check_me" />
    <input type="submit" name="btnSubmit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>​

我做了两处更改:

  1. 我将操作更改为 jsfiddle 兼容性的 javascript 调用
  2. 我将按钮名称修改为除提交之外的其他名称

我必须做#2 因为调用 $('#myform').submit 引用的是按钮,而不是提交方法,并且 jQuery 遇到了各种困惑。通过重命名按钮,$('#myform').submit 仍然是预期的 jQuery 提交函数,每个人都很高兴。

我通过几次迭代偶然发现了这一点,我将其保留在下面以供后代使用。

祝你好运!

=======下面的原始帖子=======

如果您只想“解决这个问题”,您可以重构如下:

HTML

<form id="myform" action="#" method="post">
    <input type="text" name="check_me" />
    <input type="button" id="btnSubmit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>​

JavaScript

$(document).ready(function(){
    var submitForm = $('#myform');
    submit = false;

    $("#confirm").dialog({
        resizable: false,
        height: 140,
        modal: true,
        autoOpen: false,
        buttons: {
            'Submit': function() {
                $(this).dialog('close');
                submit = true;
                submitForm.submit();
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#confirm").parent().appendTo($("#myform")); 

    $("#btnSubmit").click(function() {
        $("#confirm").dialog('open');
    });

    submitForm.submit(function() {
        if (submit) {
            return true;
        }
    });
});​

奇怪的是,下面的代码有效还有:

HTML

<form id="myform" action="javascript:alert('success!');" method="post">
    <input type="text" name="check_me" />
    <input type="button" id="btnSubmit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>​

JavaScript

$(document).ready(function(){
    var submitForm = $('#myform');
    submit = false;

    $("#confirm").dialog({
        resizable: false,
        height: 140,
        modal: true,
        autoOpen: false,
        buttons: {
            'Submit': function() {
                $(this).dialog('close');
                submit = true;
                $('#myform').submit();
                //submitForm.submit();
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#confirm").parent().appendTo($("#myform")); 
    $("#btnSubmit").click(function() { $('#myform').submit(); });

    submitForm.submit(function() {
        if (submit) {
            return true;
        } else {
            $("#confirm").dialog('open');
            return false;
        }
    });
});​

我在这里唯一改变的是删除了提交按钮和 100% 通过 jQuery 提交。

The solution is simply... your button name. Why you ask? I shall show you!

Take this jsfiddle and notice I only made a few changes. The javascript has not changed, just the HTML

HTML

<form id="myform" action="javascript:alert('Success!')" method="post">
    <input type="text" name="check_me" />
    <input type="submit" name="btnSubmit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>​

I've made two changes:

  1. I changed the action to be a javascript call for jsfiddle compatibility
  2. I modified your button name to something other than submit

I had to do #2 because calling $('#myform').submit was referencing the button, not the submit method, and jQuery got all kinds of confused. By renaming your button, the $('#myform').submit remains the expected jQuery submit function and everybody is happy.

I stumbled upon this through going through several iterations, which I've kept below for posterity.

Good luck!

=======ORIGINAL POST BELOW========

If all you want to do is "solve this", you can refactor as follows:

HTML

<form id="myform" action="#" method="post">
    <input type="text" name="check_me" />
    <input type="button" id="btnSubmit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>​

JavaScript

$(document).ready(function(){
    var submitForm = $('#myform');
    submit = false;

    $("#confirm").dialog({
        resizable: false,
        height: 140,
        modal: true,
        autoOpen: false,
        buttons: {
            'Submit': function() {
                $(this).dialog('close');
                submit = true;
                submitForm.submit();
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#confirm").parent().appendTo($("#myform")); 

    $("#btnSubmit").click(function() {
        $("#confirm").dialog('open');
    });

    submitForm.submit(function() {
        if (submit) {
            return true;
        }
    });
});​

Oddly, the below works as well:

HTML

<form id="myform" action="javascript:alert('success!');" method="post">
    <input type="text" name="check_me" />
    <input type="button" id="btnSubmit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>​

JavaScript

$(document).ready(function(){
    var submitForm = $('#myform');
    submit = false;

    $("#confirm").dialog({
        resizable: false,
        height: 140,
        modal: true,
        autoOpen: false,
        buttons: {
            'Submit': function() {
                $(this).dialog('close');
                submit = true;
                $('#myform').submit();
                //submitForm.submit();
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#confirm").parent().appendTo($("#myform")); 
    $("#btnSubmit").click(function() { $('#myform').submit(); });

    submitForm.submit(function() {
        if (submit) {
            return true;
        } else {
            $("#confirm").dialog('open');
            return false;
        }
    });
});​

The only thing I changed here was removed the submit button and 100% submit via jQuery.

久随 2024-10-20 02:47:38

将提交按钮的名称属性从“submit”更改为“btnSubmit”。

<input type="submit" name="btnSubmit" value="Go!" />

将表单操作属性中的“#”替换为空白或任何其他有效的 URL。

Change the name attribute of your submit button from 'submit' to 'btnSubmit'

<input type="submit" name="btnSubmit" value="Go!" />

Replace the '#' in the form action attribute to blank or any other valid url.

朱染 2024-10-20 02:47:38

您可以尝试在提交事件表单上添加不同类型的事件。

onsubmit="return confirm('Please confirm that you want to submit')"

请参阅此 编辑 ,现在您可以使用模态对话框返回更改创建一个函数是真是假。

You can try on submit event form have different type of events.

onsubmit="return confirm('Please confirm that you want to submit')"

see this EDIT , now you can change create one function with Modal Dialog return true or false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文