如何使用JQuery简单地获取GET
和POST
值?
我想做的是这样的事情:
$('#container-1 > ul').tabs().tabs('select', $_GET('selectedTabIndex'));
答案 0 :(得分:166)
对于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);
答案 1 :(得分:15)
有一个jQuery插件可以获得名为.getUrlParams
的GET参数对于POST,唯一的解决方案是使用PHP将POST回显到javascript变量中,就像Moran建议的那样。
答案 2 :(得分:6)
为什么不使用好的旧PHP?例如,假设我们收到一个GET参数'target':
function getTarget() {
var targetParam = "<?php echo $_GET['target']; ?>";
//alert(targetParam);
}
答案 3 :(得分:5)
或者你可以使用这个http://plugins.jquery.com/project/parseQuery,它比大多数(缩小449字节)小,返回一个表示名称 - 值对的对象。
答案 4 :(得分:3)
使用任何服务器端语言,您必须将POST变量发送到javascript。
<强> .NET 强>
var my_post_variable = '<%= Request("post_variable") %>';
小心空值。如果您尝试发出的变量实际为空,则会出现javascript语法错误。如果你知道它是一个字符串,你应该用引号括起来。如果它是一个整数,你可能想在将该行写入javascript之前测试它是否确实存在。
答案 5 :(得分:2)
这里收集全局对象中的所有GET
变量,这是一个经过几年优化的例程。自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 :
}
希望这会有所帮助。 ;)
答案 6 :(得分:2)
您可以尝试jQuery的Query String Object插件。
答案 7 :(得分:1)
jQuery插件看起来不错,但我需要的是一个快速的js函数来解析get参数。 这是我发现的。
答案 8 :(得分:1)
如果你的$ _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]);
}
});
答案 9 :(得分:1)
简单,但从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;
}
我发现它在互联网上的某个地方,只修复了一些错误
答案 10 :(得分:1)
使用以下功能:
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;
};
并将变量访问为vars['index']
,其中'index'
是get变量的名称。
答案 11 :(得分:0)
为了记录,我想知道这个问题的答案,所以我使用了PHP方法:
<script>
var jGets = new Array ();
<?
if(isset($_GET)) {
foreach($_GET as $key => $val)
echo "jGets[\"$key\"]=\"$val\";\n";
}
?>
</script>
这样,我之后运行的所有javascript / jquery都可以访问jGets中的所有内容。我觉得这是一个很好的解决方案。
答案 12 :(得分:0)
我的方法:
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]);
})();
答案 13 :(得分:0)
保持简单
用变量的键替换 VARIABLE_KEY 以获取其值
var get_value = window.location.href.match(/(?<=VARIABLE_KEY=)(.*?)[^&]+/)[0];