2015-04-22 79 views
0

我使用jquery從URL中檢索參數。可能有多個參數需要檢索。然後我將這些參數一個接一個地輸入到一個$ .post程序中。我如何檢索郵政編碼內的參數?如何從jquery post中檢索參數

var sPageURL = window.location.search.substring(1); 
     var sURLVariables = sPageURL.split('&'); 

     for (var i = 0; i < sURLVariables.length; i++) 
     { 
      $.post('php/get_victim.php', {name:sURLVariables[i]}, function(output){ 
       //I want to get the "name" variable inside here 
      }); 
     } 
+0

使用封閉...(更好的是,重構AJAX調用打一個電話,用列表,而不是循環) – tymeJV

+0

@tymeJV:不確定你的意思。你能顯示代碼嗎? – LauraNMS

回答

1

您可以使用關閉和保留變量被環繞在:

for (var i = 0; i < sURLVariables.length; i++) { 
    (function(name) { 
     $.post('php/get_victim.php', {name:name}, function(output) { 
      //I want to get the "name" variable inside here 
      console.log(name); //name from above 
     }); 
    })(sURLVariables[i]) 
} 
+0

謝謝!這有幫助! – LauraNMS