截取表单,添加数据,ajax,然后提交. [英] Intercept form, add data, ajax, then submit.

查看:89
本文介绍了截取表单,添加数据,ajax,然后提交.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拦截表单提交,将其ajax到一个位置,然后发布表单.以下是我的尝试,我的失败已被注释掉.与往常一样,对我要去哪里的任何见解将不胜感激.

I'd like to intercept a form submission, ajax it to one location and then post the form. Below is my attempt with my failures commented out. Any insight into where I'm going wrong would be appreciated, as always.

关于我为什么要做的事情的注释:我想存储表单,HTML和所有表单的完整副本,但仍将所选字段作为典型表单提交进行处理.我的ajax按预期保存了HTML,但是停止了表单提交.删除preventDefault()会使表单在ajax成功之前提交.

A note into why I'm doing what I'm doing: I would like to store a complete copy of the form, HTML and all, but still process selected fields as a typical form submit. My ajax saves the HTML as intended, but stops the form submission. Removing the preventDefault() causes the form to submit before the ajax is successful.

$('#myForm').submit(function(event) {
    event.preventDefault();
    $.ajax({
        type: 'POST',
        url: '../../lib/processCompleted.php',
        cache: false,
        dataType: 'text',
        data: { PracticeID: PracticeID },

        success: function (data) {
            alert('success');
            // I would then like to submit the form as usual.
            //return true; 
            //$('#myForm').submit();
        },
        error: function () {
            alert('boo');
        }
    });
});

欢呼

推荐答案

再次调用$('#myForm').submit();时,将调用提交处理程序,这将阻止表单提交.在这种情况下,将创建一个无限循环的ajax请求.

when you call $('#myForm').submit(); again the submit handler will get called which will prevent the form submission. In this case creating a infinite loop of the ajax requests.

尝试

$('#myForm')[0].submit();

演示:小提琴

这篇关于截取表单,添加数据,ajax,然后提交.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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