答案 0 :(得分:1)
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
} ();
alert(QueryString.num1);
答案 1 :(得分:1)
我觉得用javascript获取get参数有点混淆了PHP和javascript的角色所以我更喜欢这样做。您可以使用window.href
获取并解析网址,但这是更好的格式
PHP正文中的某个地方:
echo '<input type="hidden" id="num1_arg" value=" . $_GET['num1'] . '/>';
echo '<input type="hidden" id="num2_arg" value=" . $_GET['num2'] . '/>';
Javascript(生病使用jquery,但可以不用)
n1 = $('#num1_arg').val();
n2 = $('#num2_arg').val();
答案 2 :(得分:0)
看一下这篇文章 - parseUri
答案 3 :(得分:0)
尝试使用这个简单的功能:
var parseQueryString = function() {
var queryString = window.location.search.substring(1);
var pairs = queryString.split("&");
var params = {};
for (var i = 0; i < pairs.length; i++) {
var parts = pairs[i].split("=");
var key = parts[0];
var value = parts[1];
params[key] = value;
}
return params;
};
答案 4 :(得分:0)
如果num1和num2的顺序相同,则可以使用regular expressions:
var url = ...
var pattern = /num1=(\d*)&num2=(\d*)/
var match = pattern.exec(url)
var num1 = match[1]
var num2 = match[2]