如何使用JQuery获取GET和POSTvariables?

我如何简单地获得与JQuery的GETPOST值?

我想要做的是这样的:

 $('#container-1 > ul').tabs().tabs('select', $_GET('selectedTabIndex')); 

对于GET参数,您可以从document.location.search抓取它们:

 var $_GET = {}; document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () { function decode(s) { return decodeURIComponent(s.split("+").join(" ")); } $_GET[decode(arguments[1])] = decode(arguments[2]); }); document.write($_GET["test"]); 

对于POST参数,可以将JSON格式的$_POST对象序列化为<script>标签:

 <script type="text/javascript"> var $_POST = <?php echo json_encode($_POST); ?>; document.write($_POST["test"]); </script> 

当你在(在服务器端做事)时,你也可以在PHP上收集GET参数:

 var $_GET = <?php echo json_encode($_GET); ?>; 

注意:您需要使用PHP版本5或更高版本才能使用内置的json_encode函数。


更新:这是一个更通用的实现:

 function getQueryParams(qs) { qs = qs.split("+").join(" "); var params = {}, tokens, re = /[?&]?([^=]+)=([^&]*)/g; while (tokens = re.exec(qs)) { params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]); } return params; } var $_GET = getQueryParams(document.location.search); 

有一个jQuery的插件来获得称为.getUrlParams的 GET参数

对于POST来说,唯一的解决方法是使用PHP将POST回传到一个javascriptvariables中,就像Moran所build议的那样。

为什么不使用良好的旧的PHP? 例如,让我们说我们收到一个GET参数“target”:

 function getTarget() { var targetParam = "<?php echo $_GET['target']; ?>"; //alert(targetParam); } 

或者你可以使用这个http://plugins.jquery.com/project/parseQuery ,它比最小(缩小449字节),返回一个代表名称 – 值对的对象。

使用任何服务器端语言,您将不得不将POSTvariables发送到JavaScript。

。净

 var my_post_variable = '<%= Request("post_variable") %>'; 

只要注意空值。 如果你试图发出的variables实际上是空的,你将会得到一个javascript语法错误。 如果你知道这是一个string,你应该把它包装在引号中。 如果它是一个整数,你可能需要testing,看看它是否真的存在之前写行到JavaScript。

你可以试试jQuery的查询string对象插件。

这里有一些东西来收集全局对象中的所有GETvariables,这是一个经过数年优化的例程。 自从jQuery的兴起以来,现在把它们存储在jQuery本身当中似乎是适当的,我们正在与John一起检查潜在的核心实现。

 jQuery.extend({ 'Q' : window.location.search.length <= 1 ? {} : function(a){ var i = a.length, r = /%25/g, // Ensure '%' is properly represented h = {}; // (Safari auto-encodes '%', Firefox 1.5 does not) while(i--) { var p = a[i].split('='); h[ p[0] ] = r.test( p[1] ) ? decodeURIComponent( p[1] ) : p[1]; } return h; }(window.location.search.substr(1).split('&')) }); 

用法示例:

 switch ($.Q.event) { case 'new' : // http://www.site.com/?event=new $('#NewItemButton').trigger('click'); break; default : } 

希望这可以帮助。 ;)

jQuery插件看起来不错,但是我需要的是一个快速的js函数来parsingget参数。 这是我发现的。

http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx

如果你的$ _GET是多维的,这可能是你想要的:

 var $_GET = {}; document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () { function decode(s) { return decodeURIComponent(s.split("+").join(" ")); } //handling for multidimensional arrays if(decode(arguments[1]).indexOf("[]") > 0){ var newName = decode(arguments[1]).substring(0, decode(arguments[1]).length - 2); if(typeof $_GET[newName] == 'undefined'){ $_GET[newName] = new Array(); } $_GET[newName].push(decode(arguments[2])); }else{ $_GET[decode(arguments[1])] = decode(arguments[2]); } }); 

简单,但有用的从url获得瓦尔/值:

 function getUrlVars() { var vars = [], hash, hashes = null; if (window.location.href.indexOf("?") && window.location.href.indexOf("&")) { hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); } else if (window.location.href.indexOf("?")) { hashes = window.location.href.slice(window.location.href.indexOf('?') + 1); } if (hashes != null) { for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars[hash[0]] = hash[1]; } } return vars; } 

我发现它在互联网上的某个地方,只是修复了一些错误

使用以下function:

 var splitUrl = function() { var vars = [], hash; var url = document.URL.split('?')[0]; var p = document.URL.split('?')[1]; if(p != undefined){ p = p.split('&'); for(var i = 0; i < p.length; i++){ hash = p[i].split('='); vars.push(hash[1]); vars[hash[0]] = hash[1]; } } vars['url'] = url; return vars; }; 

并访问variables作为vars['index']其中'index'是variables的名称。

只是为了logging,我想知道这个问题的答案,所以我用了一个PHP方法:

 <script> var jGets = new Array (); <? if(isset($_GET)) { foreach($_GET as $key => $val) echo "jGets[\"$key\"]=\"$val\";\n"; } ?> </script> 

这样,我所有的javascript / jquery都可以访问jGets中的所有东西。 我觉得它是一个很好的优雅

我的方法是:

 var urlParams; (window.onpopstate = function () { var match, pl = /\+/g, Regex for replacing addition symbol with a space search = /([^&=]+)=?([^&]*)/g, decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, query = window.location.search.substring(1); urlParams = {}; while (match = search.exec(query)) urlParams[decode(match[1])] = decode(match[2]); })();