2013-11-27 94 views
0

我只是不明白爲什麼這個表單發送同一電子郵件的多個副本(從1-8個副本的任何地方)。我盡力解決問題,但我無法弄清楚。jQuery Ajax多次提交多次

我使用jQuery驗證引擎來驗證表單,如果正確驗證,我用jquery.post()提交表單。

我包括我的所有代碼,只是爲了安全起見。但是有些東西可能並不重要,比如我在消息成功發送時加載到iframe中的ppcconversion腳本。

請參見下面的代碼:

$(".consult-form form .submit").click(function(){ 
    $(".consult-form form").validationEngine('attach', { 
     promptPosition : "topLeft", 
     onValidationComplete: function(contactForm, status){ 
      if(status==true){ 
       $(".consult-form form .submit").clone().insertAfter($(this)).attr("disabled","true"); 
       $(".consult-form form .submit").hide(); 
       _gaq.push(['_trackPageview', '/web-contact']); // Google Page Tracker 
       $.post('/path/to/process.php', $(".consult-form form").serialize(), function(data) { 
        // Add Thank You Message 
        $('.thank-you-message').html(data); 
        // Create IFRAME to page with Adwords Tracking Script 
        function ppcconversion() { 
         var iframe = document.createElement('iframe'); 
         iframe.style.width = '0px'; 
         iframe.style.height = '0px'; 
         document.body.appendChild(iframe); 
         iframe.src = 'http://www.fullurlto.com/conversion-script.php'; 
        }; 
        ppcconversion(); 
       }); 
       return false; // I attempted to add this line to prevent it from submitting multiple times. It didn't work. 
       $(".consult-form form").hide(); 
      } 
     } 
    }); 
}); 

如果有您需要更深入的瞭解只是讓我知道的任何細節,我會爲他們提供。

謝謝!

回答

0

現在你在ready處理

$(function() { 
    $(".consult-form form").validationEngine('attach', { 
     //... 
    } 
}); 
+0

這很有道理。我明白你的建議,但我不確定如何相應地編輯我的代碼。對不起,我不是很有經驗。你能在你的答案中提供更詳細的例子嗎? – norsewulf

+0

更換'$( 「CONSULT-form表單.submit」)。點擊(函數(){'和'$(函數(){' – Andreas

+0

哦哈哈,沒關係很容易的。我做了更新,我的代碼。我認爲這可能是問題,但我會在一些測試後將它作爲正確的答案。謝謝! – norsewulf

0

jQuery驗證引擎通常觸發提交表單上添加的.submit

每次點擊一個新的驗證處理程序而不是添加驗證一次,所以沒有必要綁定提交按鈕點擊事件。

只要堅持連接到結尾的形式驗證和正常提交。應該很好(未測試)

+0

很酷。我做了Andreas建議的改變(與你提到的一樣)。希望這可以解決我的問題。 – norsewulf