2016-04-25 111 views
0

我增加了郵件訂閱彈出形式,它是在提交發送兩次,也正在顯示的確認消息的兩倍聯繫表格提交兩次

下面是代碼:

$(document).ready(function() { 
    $(".modalbox").fancybox(); 
    $("#contact").submit(function(e) {  

     e.preventDefault();     

     var emailval = $("#email").val(); 

     var mailvalid = validateEmail(emailval); 

     if(mailvalid == false) { 
      $("#email").addClass("error"); 
     } 
     else if(mailvalid == true){ 
      $("#email").removeClass("error"); 
     } 


     if(mailvalid == true) { 
      // if both validate we attempt to send the e-mail 
      // first we hide the submit btn so the user doesnt click twice 
      $("#send").replaceWith("<em>sending...</em>"); 

      $.ajax({ 
       type: 'POST', 
       url: 'sendmessage.php', 
       data: $("#contact").serialize(), 
       success: function(data) { 
        if(data == "true") { 
         $("#contact").fadeOut("fast", function(){ 
          $(this).before("<p><strong>Success! You have signed up for a trial. A member of our team wil soon be in contact :)</strong></p>"); 
          setTimeout("$.fancybox.close()", 1700); 
         }); 
        } 
       } 
      }); 
     } 
    }); 
}); 

回答

1

我不「噸認爲有任何必要寫這行:

$("#contact").submit(function() { return false; }); // Remove this 

您可以刪除此行:

$("#send").on("click", function(){  // Remove this 

而且寫來代替:

$("#contact").submit(function(e) {  // Add this 

    e.preventDefault();     // Add this 

    var emailval = $("#email").val(); 

    var mailvalid = validateEmail(emailval); 

    /* Other code */ 

}); 
+0

非常感謝您! 它一開始並沒有工作,我以前在網上瀏覽,看到一些人把e.stopImmediatePropagation();在e.preventDefault()下;這對我有用。 再次感謝:) – Ashton

+0

請務必投票並接受答案,如果它解決您的問題。 :) –