Javascript 表格不';不要在回调时提交

Javascript 表格不';不要在回调时提交,javascript,jquery,ajax,forms,Javascript,Jquery,Ajax,Forms,我正在尝试在ajax调用成功完成后发送电子邮件。我无法访问正在进行AJAX调用的文件 我正在阻止第一次提交,进行ajax调用,并在竞争时再次提交表单。这样做时,我似乎不明白为什么我必须按两次提交按钮才能发送电子邮件 这是我的密码: "use strict"; var submitted = ''; /* Send info to the portal */ jQuery(function($) { $('#quote').submit(function(e){ var

我正在尝试在ajax调用成功完成后发送电子邮件。我无法访问正在进行AJAX调用的文件

我正在阻止第一次提交,进行ajax调用,并在竞争时再次提交表单。这样做时,我似乎不明白为什么我必须按两次提交按钮才能发送电子邮件

这是我的密码:

"use strict";
var submitted = '';

/* Send info to the portal */
jQuery(function($) {
    $('#quote').submit(function(e){
        var firstName   = $('#firstName').val(),
            lastName    = $('#lastName').val(),
            email       = $('#email').val(),
            phone       = $('#primaryPhone').val(),
            weight      = $('#weight').val(),
            origin      = $('#originPostalCode').val(),
            destination = $('#destinationPostalCode').val(),
            notes       = $('#whatsTransported').val()
        if( submitted != 1 )
        {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "https://somewhere.on/the/internet.php",
                crossDomain: true,
                dataType: "json",
                data: {"key": "my secret key","first": firstName, "last": lastName, "email": email, "phone": phone, "weight": weight, "origin_postal": origin, "dest_country": destination, "note": notes }
            })
            .done(function(data){
                if(data[1][0][0] == 2)
                {
                    $('.alert').append( data[1][0][1].message ).addClass('alert-error').show();
                } else if(data[1][0][0] == 0) {
                    console.log("Made it.");
                    $('#quote #submit').submit();
                } else {
                    $('.alert').append( "Appologies, it seems like something went wrong. Please, <strong>call (877) 419-5523</strong> for immediate assistance or a free quote.");
                }
            })
            .fail(function(data) { console.log(data); });
        }
        submitted = '1';
    });
});
“严格使用”;
提交的风险值=“”;
/*将信息发送到门户*/
jQuery(函数($){
$('#quote')。提交(函数(e){
var firstName=$('#firstName').val(),
lastName=$('#lastName').val(),
email=$('#email').val(),
phone=$(“#primaryPhone”).val(),
权重=$(“#权重”).val(),
origin=$('#originPostalCode').val(),
destination=$('#destinationPostalCode').val(),
notes=$('#whatsTransported').val()
如果(已提交!=1)
{
e、 预防默认值();
$.ajax({
类型:“POST”,
url:“https://somewhere.on/the/internet.php",
跨域:是的,
数据类型:“json”,
数据:{“key”:“my secret key”,“first”:firstName,“last”:lastName,“email”:email,“phone”:phone,“weight”:weight,“origin_postal”:origin,“dest_country”:destination,“note”:notes}
})
.完成(功能(数据){
如果(数据[1][0][0]==2)
{
$('.alert').append(数据[1][0][1]。message.addClass('alert-error').show();
}else if(数据[1][0][0]==0){
console.log(“成功了”);
$('#quote#submit').submit();
}否则{
$('.alert').append(“抱歉,好像出了什么问题。请,拨打(877)419-5523寻求即时帮助或免费报价。”);
}
})
.fail(函数(数据){console.log(数据);});
}
提交='1';
});
});
这是表单HTML

<form action="<?php echo bloginfo($show='template_url').'/includes/form-email.php'; ?>" class="span6 offset1" id="quote" method="post">
            <div class="row formRow">
                <div class="firstName span3">
                    <label for="firstName"><?php _e('First Name:','StreamlinedService'); ?></label>
                    <input type="text" name="firstName" id="firstName">
                </div>
                <div class="lastName span3">
                    <label for="lastName"><?php _e('Last Name:','StreamlinedService'); ?></label>
                    <input type="text" name="lastName" id="lastName">
                </div>
            </div>
            <div class="row formRow">
                <div class="email span3">
                    <label for="email"><?php _e('Email Address:','StreamlinedService'); ?></label>
                    <input type="text" name="email" id="email">
                </div>
                <div class="primaryPhone span3">
                    <label for="primaryPhone"><?php _e('Phone Number:','StreamlinedService'); ?></label>
                    <input type="text" name="primaryPhone" id="primaryPhone">
                </div>
            </div>
            <div class="row formRow">
                <div class="weight span2">
                    <label for="weight"><?php _e('Weight (lbs):','StreamlinedService'); ?></label>
                    <input type="text" name="weight" id="weight">
                </div>
            </div>
            <div class="row formRow">
                <div class="originPostalCode span3">
                    <label for="originPostalCode"><?php _e('Origin:','StreamlinedService'); ?></label>
                    <input type="text" name="originPostalCode" id="originPostalCode">
                </div>
                <div class="destinationPostalCode span3">
                    <label for="destinationPostalCode"><?php _e('Destination:','StreamlinedService'); ?></label>
                    <input type="text" name="destinationPostalCode" id="destinationPostalCode">
                </div>
            </div>
            <div class="row">
                <div class="whatsTransported span6">
                    <label for="whatsTransported"><?php _e('What can we help you transport?','StreamlinedService'); ?></label>
                    <textarea name="whatsTransported" id="whatsTransported" rows="5"></textarea>
                </div>
                <input type="hidden" name="formType" value="quote" />
                <input type="hidden" name="siteReferer" value="<?php echo $blog_id ?>">
                <input type="submit" id="submit" name="submit" value="<?php _e('Get Freight Quote','StreamlinedService') ?>" class="btn btn-primary btn-large span3 offset3" style="float:right;">
            </div>
        </form>

您使用了错误的Jquery方法
你错过了关键:写得少做得多,JQuery的核心。
无论如何,试试这个:

"use strict";
/* Send info to the portal */
jQuery(function($) {
    $('#quote').submit(function(e){
         var tis = $(this);
         $.ajax({
                type: "POST",
                url: "https://somewhere.on/the/internet.php",
                cache: true,
                dataType: "json",
                data: tis.serialize(),
                success: function(data){
                   if(data[1][0][0] == 2){
                    $('.alert').append( data[1][0][1].message ).addClass('alert-error').show();
                } else if(data[1][0][0] == 0) {
                    console.log("Made it.");
                    $('#quote #submit').submit();
                } else {
                    $('.alert').append( "Appologies, it seems like something went wrong. Please, <strong>call (877) 419-5523</strong> for immediate assistance or a free quote.");
                }
                },
                error: function(data){console.log(data);}
            });
         e.stroPropagation();
         e.preventDefault();
    });
});
“严格使用”;
/*将信息发送到门户*/
jQuery(函数($){
$('#quote')。提交(函数(e){
var tis=$(本);
$.ajax({
类型:“POST”,
url:“https://somewhere.on/the/internet.php",
是的,
数据类型:“json”,
数据:tis.serialize(),
成功:功能(数据){
如果(数据[1][0][0]==2){
$('.alert').append(数据[1][0][1]。message.addClass('alert-error').show();
}else if(数据[1][0][0]==0){
console.log(“成功了”);
$('#quote#submit').submit();
}否则{
$('.alert').append(“抱歉,好像出了什么问题。请,拨打(877)419-5523寻求即时帮助或免费报价。”);
}
},
错误:函数(数据){console.log(数据);}
});
e、 频闪传播();
e、 预防默认值();
});
});

最后一点。。无法请求不在脚本的同一域上托管的远程页面。。为此,只需在表单节点上直接使用
.submit
(注意
[0]

变成

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

这将绕过jQuery绑定事件并强制回发。

发布表单的HTML请避开您的问题,我建议使用布尔值将
submitted
变量设置为true或false,而不是
字符串
1或空。已添加。”quote'是表单的id,'submit'是submit按钮的id。被接受的答案的讨论是一些有趣的见解和解决方案。他可以在这里“写得少一些:$”(“#quote#submit”),这充其量是选择$(“#submit”)的一种效率较低的方法,对,他可以在代码中优化很多东西。我只编辑了它的一部分。请随意编辑我的代码:-)大部分代码都是我试图将它们分开,以确保我没有遗漏一些愚蠢的东西。我在身份证上翻了一番,以确保没有其他冲突。谢谢你的评论。我似乎不明白为什么表单没有在
$(“#quote#submit”).submit()上提交另外,我的服务器正在接受来自本地计算机的跨域请求。@ClaudioLudovicoPanetta这不会以无限循环结束吗?ajax调用将继续一次又一次地提交,因为无法判断在
submit()
成功后ajax请求是否已经运行:
未捕获类型错误:object的属性“submit”不是函数
是我收到的错误。这没有任何意义。。。所有表单元素都应该有一个提交方法
document.createElement(“表单”).submit
->
函数submit(){[native code]}
我正在寻找这篇文章,但我记得有一个错误记录,要求您在按钮上执行submit方法,而不是表单。是jquery错误吗?或者本地javascript。我提出的建议使用本机DOM方法,与jQuery无关。我从未听说过必须通过按钮提交。“表单及其子元素不应使用与表单属性(如submit、length或method)冲突的输入名称或ID。名称冲突可能会导致混乱的失败。”这可能是问题的一部分,但如您所述,我们使用的是本机DOM方法,所以我不知道为什么它不起作用。
$('#quote')[0].submit();