2016-08-24 51 views
1

我有一個表單使用AJAX請求提交五次。我在這些表單標籤之外有一個值,referer_fname,我想附加到每個AJAX請求。我怎樣才能做到這一點?如何使用序列化AJAX提交表單標籤外部輸入字段

下面是我的腳本至今:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script> 
    <!--serial submit ajax--> 
    <script> 
    function mySubmit(){ 
    var myForms = document.getElementsByTagName("form"); 
    for (var t = 0; t<myForms.length; t++){ 
    $.post("http://", $(myForms).eq(t).serialize(), function (data, status) { 
     if (status === "success"){ 
    window.location.href= "http://redirect.com"; 
    } 
    } 
    ) 
    } 
    } 

    </script> 

回答

1

您可以附加到序列化表單域輸出(見this the accepted answer on this post for reference

function mySubmit(){ 
    var myForms = $("form"); 
    myForms.each(function(index) { 
    var form = myForms.eq(index); 
    var serializedForm = form.serialize(); 
    serializedForm += '&referer_fname='+$('#refererFrame').val(); 
    $.post("https://", serializedForm, function (data, status) { 
     if (status === "success"){ 
     window.location.href= "http://redirect.com"; 
     } 
    }); 
    }); 
} 
+0

謝謝!這工作 – badsmell