这是我使用。
<!doctype html>
<html>
<head>
<script src="jquery-1.4.js"></script>
<script>
/**
* @return null if param not found
* @return string if param found once
* @return array if param found more than once or name ends with "[]"
*/
function getQueryStringParam (name) {
var result = null;
if (location.search !== "") {
var querystring = location.search.substring(1).replace(/\+/g, " ");
var pairs = querystring.split("&");
var isFound = false;
for (var i=0; i<pairs.length; i++) {
var pair = pairs[i].split("=");
var key = decodeURIComponent(pair[0]);
if (key === name) {
var val = (pair.length === 2 ? decodeURIComponent(pair[1]) : key);
if (!isFound) {
isFound = true;
result = [val];
}
else {
result.push(val);
}
}
}
if (isFound && result.length == 1 && !/\[.*\]$/.test(name)){
result = result[0];
}
}
return result;
}
/**
* helper function
*/
function printQueryStringParam (name) {
var result = "";
var val = getQueryStringParam(name);
if (val === null) {
result = "null";
}
else if (jQuery.isArray(val)) {
result = "[\n\t" + val.join(",\n\t") + "\n]";
}
else {
result = val;
}
return result;
}
</script>
</head>
<body>
<form action="test.html" method="get">
<p>
text <input type="text" name="text"
value="space: , plus:+, slash:/, backslash:\, amp:&, equals:=, less:<, greater:>"><br>
</p>
<p>
checkbox[]<br>
<input type="checkbox" name="checkbox[]" value="a" checked> a<br>
<input type="checkbox" name="checkbox[]" value="b" checked> b<br>
</p>
<p>
<input type="submit">
</p>
</form>
<hr>
<pre>
<script>
document.write("text: "+printQueryStringParam("text"));
document.write("\ncheckbox[]: "+printQueryStringParam("checkbox[]"));
</script>
</pre>
</body>
</html>
谢谢。当我回去工作时我会尝试。我试图在本地文件而不是通过HTTP,但我想知道这是否是真正的问题。与此同时,我通过获取location.search的子字符串来解决此问题。 – 2010-01-15 16:48:04