2016-04-04 71 views
0

我想提交表单#mc-embedded-subscribe-form在ajax成功函数后,但它似乎不工作。当我在函数成功之后提醒“OK”时,它工作正常,但是当我尝试启动de提交函数时,它不会。谢谢!Ajax回调后提交表格

<form action="//google.us12.mailchimp.com/subscribe/" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate> 

<input type="email" id="mce-EMAIL" name="EMAIL" value="" placeholder="Email..."/> 

</form> 
<div class="email_submit" id="btn_email" style="">Subscribe</div> 

AJAX

$("#btn_email").click(function() { 

$('#btn_email').hide(); 
$('#btn_email_load').show(); 

var email = $('#mce-EMAIL').val(); 

$.ajax({ 

     type: "POST", 
     url: "classes/_mailchimp.php", 
     data:{ email: email, }, 

     cache: false, 

     success:function(data){ 

     if(data == 'ok'){ 

     $("form#mc-embedded-subscribe-form").submit(); 
     //alert("OK") 

     return false; 
     } 

     alert(data); 
     return false; 
} 
}); 
+0

是响应defo'ok' – John

+0

您是否收到错误或者没有任何操作? – Raif

+0

if(data =='ok'){else else {alert('data is not ok'); } – John

回答

0

我终于找到了解决方案,使用此问题“异步:假,”到AJAX调用。谢谢。

$.ajax({ 

     type: "POST", 
     url: "classes/_mailchimp.php", 
     data:{ email: email, }, 
     async: false, //solution 
     cache: false, 

     success:function(data){ 

     if(data == 'ok'){ 

     $("form#mc-embedded-subscribe-form").submit(); 
     //alert("OK") 

     return false; 
     } 

     alert(data); 
     return false; 
} 
}); 
0

嘛。首先尝试删除表单中的方法和动作attribut,因为如果您已经使用ajax,则不是必须的。

  1. 重写表单属性,把一个真正的输入按钮,如:

    <form id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate"> 
    <!--We remove too the value attribute because is innecessary--> 
    <input type="email" id="mce-EMAIL" name="EMAIL" placeholder="Email..."/> 
    
    <input type="button" class="email_submit" id="btn_email" value="Suscribe"> </form> 
    
        <!--We are creating another div that it will show when #btn_email has been listened--> <div id="#btn_email_load" style="display:none"> 
    
  2. 请留意的是我把表单中的按钮,而不是在那里。所以,下一个是你的ajax代码。变化和误差修改被注释掉

    $("#btn_email").click(function() { 
    
        $('#btn_email').hide(); 
        $('#btn_email_load').show(); 
    
        var email = $('#mce-EMAIL').val(); 
        $values = 'mail='+email; 
         $.ajax({ 
           type: "POST", 
           url: "classes/_mailchimp.php", 
           //Save all the data in a variable and not an object, if you want to save it with an object, use the short .post() ajax method to do it. In this case, we gonna save the data in a variable before declared as $values 
           data:$values, 
           cache: false, 
    
           success:function(data){ 
    
           if (data == 'ok'){ 
            alert('ok') 
           } 
         }); //close your ajax petition 
    });