jQuery Submit() 和 AJAX 事件;如何触发常规表单提交?

发布于 2025-01-08 13:10:33 字数 1255 浏览 5 评论 0原文

我正在开发一个项目,当单击提交按钮时,我会捕获该事件并通过外部服务运行表单以检查某些内容是否有效。

如果出现问题,我会弹出一个对话框,告诉用户出现问题,并为他们提供覆盖以继续的选项。

问题是如何触发表单提交。如果我只使用 .submit(),它会再次将表单发送到对话框捕获中。

$('#editAddr').submit(function(){
    var checkString = addrString();
    $.getJSON('/validate.php' + checkString, function(data){
        if(data == 0){
            var confw = '';
            confw += '<div><p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>';
            confw += 'Failed validation.</p></div>';

            var $dialog = $(confw).dialog({
                title: "Validation Problem",
                modal: true, resizable: false, width: 500,
                buttons: {
                    "Edit": function(){
                        $(this).dialog('close');
                    },
                    "Override": function(){
                        $(this).dialog('close');
                        $('#editAddr').submit();
                    }
                }
            });
        }else{
            $('#editAddr').submit();            
        }
    });

    return false;
});

如何通过单击对话框按钮正确提交此表单?

编辑:确保您的表单不包含 id="submit" 的按钮,否则您将破坏 jQuery .submit()

I'm working on a project where when the submit button is clicked, I catch the event and run the form through an external service to check that something is valid.

If there is a problem, I pop a dialog telling the user that there was a problem, and give them the option of overriding to continue on.

The problem is how to trigger the form submit. If I just use .submit(), it send the form into the dialog catch again.

$('#editAddr').submit(function(){
    var checkString = addrString();
    $.getJSON('/validate.php' + checkString, function(data){
        if(data == 0){
            var confw = '';
            confw += '<div><p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>';
            confw += 'Failed validation.</p></div>';

            var $dialog = $(confw).dialog({
                title: "Validation Problem",
                modal: true, resizable: false, width: 500,
                buttons: {
                    "Edit": function(){
                        $(this).dialog('close');
                    },
                    "Override": function(){
                        $(this).dialog('close');
                        $('#editAddr').submit();
                    }
                }
            });
        }else{
            $('#editAddr').submit();            
        }
    });

    return false;
});

How can I submit this form properly through the dialog button click?

EDIT: Make sure your form does not include a button with the id="submit", or you will break the jQuery .submit()

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

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

发布评论

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

评论(5

死开点丶别碍眼 2025-01-15 13:10:33

不要绑定到表单的提交事件,而是绑定到提交按钮的单击事件。这样你就可以捕获submit事件的click事件并在需要时返回false,或者继续处理表单上的submit()事件。

​$('input[type="submit"]').click(function(e){
    e.preventDefault();
    if() // invalid data
    {

    }
    else // good to go
    {
        $('form').submit();
    }
})​

如果有人禁用了 JavaScript,这个解决方案也会大大降低性能。我读过的其他一些解决方案不允许这样做。

Instead of binding to the submit event of your form, bind to the click event of the submit button instead. This way you can catch the click event of the submit event and return false when you need to, or proceed with the submit() event on the form.

​$('input[type="submit"]').click(function(e){
    e.preventDefault();
    if() // invalid data
    {

    }
    else // good to go
    {
        $('form').submit();
    }
})​

This solution will also degrade nicely if someone disabled JavaScript. Some of the other solutions I've read won't allow this.

油饼 2025-01-15 13:10:33

只需在表单的 dom 元素上调用 .submit() 即可。

我认为你可以像这样从 jQuery 获取它:

$('#editAddr')[0].submit();  // goes in your Override button logic

Just call .submit() on the form's dom element.

I think you can get it from jQuery like this:

$('#editAddr')[0].submit();  // goes in your Override button logic
债姬 2025-01-15 13:10:33

问题出在你连接逻辑的方式上。

表单中的按钮应设为常规按钮,而不是提交按钮
那么,它的 onaction 属性应该指向上面的方法。

之后 form.submit() 应该按预期工作。

另一种选择是在方法上添加一个标志。

The issue comes from the way you wired the logic.

Your button in the form, should be made a regular button, not a submit button
then, its onaction property should be pointing to the method above.

after that form.submit() should work as expected.

The other option is to add a flag on the method.

寒冷纷飞旳雪 2025-01-15 13:10:33

您的问题是,在 Jquery 对象上调用 Submit 再次通过 jquery onsubmit 运行它。一旦你完成了验证器,你只需要在 DOM 对象上调用它 - 你可以使用

document.GetElementById(editAddr).submit();

或 with

$("#editAddr").get(0).submit();

来获得它至少,这是它与验证提交处理程序一起工作的方式。我认为它的工作方式与标准提交处理程序相同。

Your issue is that calling submit on the Jquery object runs it back through the jquery onsubmit again. Once you're done with the validator, you just need to call it on the DOM object - you can get this with

document.GetElementById(editAddr).submit();

or with

$("#editAddr").get(0).submit();

At least, this is the way it works with the validation submit handler. I presume it works the same way with the standard submit handler.

风为裳 2025-01-15 13:10:33

添加诸如 isOverride 或 isValid 布尔值之类的内容,您可以在提交函数的开头检查这些内容以绕过进一步的处理。

Add something like an isOverride or isValid boolean that you check in the beginning of the submit function to bypass further processing.

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