貝寶

2013-09-27 69 views
0

之前提交表單值我試圖覆蓋下面的貝寶提交表單提交之前,運行一些AJAX,但由於某些原因的一切行動,直到實際提交的形式工作。貝寶

寶表格

<form name="_xclick" id="payWithPaypal" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> 
    <input type="hidden" name="cmd" value="_xclick"> 
    <input type="hidden" name="business" value="[email protected]"> 
    <input type="hidden" name="currency_code" value="USD"> 
    <input type="hidden" name="item_name" value="Item Name Here"> 
    <input type="hidden" name="amount" value="12"> 
    <input type="hidden" name="return" value="http://www.website.com"> 
    <input type="hidden" name="notify_url" value="http://www.website.com/ipn.php"> 
    <input type="submit" id="submitPaypal" class="mailsubmit" value="Pay Now" border="0" name="submit"> 
</form> 

重寫提交按鈕

<script> 
    $(function(){ 
     $('#submitPaypal').on("click", function(e){ 
      event.preventDefault(); 
      tailor(); 
     }); 
    }); 
</script> 

阿賈克斯應該運行,然後提交上述形式

<script type="text/javascript"> 
     function tailor(){ 
      $.ajax({ 
       url: "info.php", 
       type: "POST", 
       data: {NAME:"test"}, 
       success: function(data){ 
        $('#payWithPaypal').submit(); 
       } 
      }); 
     } 
    </script> 

任何洞察力,爲什麼$('#payWithPaypal').submit();不能正常工作,將不勝感激

回答

-1

最後想出了這個出於某種原因,我仍然不確定爲什麼,但提交按鈕需要放在窗體的一面標籤,然後它就像它應該那樣工作。如果有人知道這是爲什麼我很想知道

4

的情況下,你event.preventDefault();綁定按鈕點擊不會阻止表單提交。這就是爲什麼你的.ajax()只有當提交按鈕放在表單之外時才起作用。

我建議這樣的事情,而不是:

<script type="text/javascript"> 

$('#payWithPaypal').submit(function(e) { 

    var self = this; 

    e.preventDefault(); 

    $.ajax({ 
     url: 'info.php', 
     type: 'post', 
     data: $(':input', self), 
     success: function(json) { 
     self.submit(); 
     } 
    }); 
}); 

</script>