jQuery 将jquery中的多个参数从一个页面传递和检索到另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16996803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing and retrieving multiple parameters in jquery from one page to another page
提问by Adi
I want to pass 4 names using jQuery from one page to another page using jQuery in winRT app. done using one parameter. How to pass multiple parameters and retrieve in 2nd page?
我想在 winRT 应用程序中使用 jQuery 将 4 个名称从一个页面传递到另一个页面。使用一个参数完成。如何传递多个参数并在第二页中检索?
Coding for one parameter.
一个参数的编码。
default.html
:
default.html
:
<html>
<head>
<script>
$(document).ready(function () {
$(function () {
$('#navigate12').click(function () {
var te = $("#text1").val();
var tea = $("#text2").val();
window.location.href = "/page.html?cal=" + te;
});
});});
</script>
</head>
<body>
<button id="navigate12">navigate</button>
<input id="text1" type="text"/>
<input id="text2" type="text"/>
<input id="text3" type="text"/>
<input id="text4" type="text"/>
</body>
</html>
page.html
<html>
<head>
<script src="/js/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
function qs() {
var qsparam = new Array();
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i = 0; i < parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0)
{
var key = parms[i].substring(0, pos);
var val = parms[i].substring(pos + 1);
qsparam[key] = val;
}
}
var msg = new Windows.UI.Popups.MessageDialog(val);
msg.showAsync();
}
var splitstr = qs();
});
</script>
<title>
</title>
</head>
<body>
</body>
</html>
回答by Filippos Karapetis
You can pass GET parameters to a page using "?param=foo" for the first parameter. Additional parameters are included with "&" instead of "?".
您可以使用“?param=foo”作为第一个参数将 GET 参数传递给页面。附加参数包含在“&”而不是“?”中。
Since you are passing GET parameters, you can put them all in the URL and read them in your target page. Alternatively, you can use POST parameters instead.
由于您正在传递 GET 参数,因此您可以将它们全部放在 URL 中并在您的目标页面中读取它们。或者,您可以改用 POST 参数。
Here's a reference on GET vs PUT parameters:
这是 GET 与 PUT 参数的参考:
http://www.w3schools.com/tags/ref_httpmethods.asp
http://www.w3schools.com/tags/ref_httpmethods.asp
If you are passing all parameters as GET, you can expand this bit:
如果您将所有参数作为 GET 传递,您可以扩展这一位:
var te = $("#text1").val();
var tea = $("#text2").val();
window.location.href = "/page.html?cal=" + te;
to something like this:
像这样:
var te = $("#text1").val();
var text2 = $("#text2").val();
var text3 = $("#text3").val();
var text4 = $("#text4").val();
window.location.href = "/page.html?cal=" + te + "&text2=" + text2 + "&text3=" + text3 + "&text4=" + text4;
As for obtaining the parameters in the other page: I am assuming that you want to obtain the GET parameters in the other page via jQuery, since your target page is plain HTML. If this is the case, check this question
至于获取其他页面中的参数:我假设您想通过jQuery获取其他页面中的GET参数,因为您的目标页面是纯HTML。如果是这种情况,请检查此问题
回答by Adi
done using this technique in default.html
在 default.html 中使用此技术完成
$(function () {
$('#navigate12').click(function () {
var te = $("#text1").val();
var text2 = $("#text2").val();
var text3 = $("#text3").val();
var text4 = $("#text4").val();
window.location.href = "/page.html?cal=" + te + "&text2=" + text2 +
"&text3=" + text3 + "&text4=" + text4; <-- this line has changes to include multiple parameters rest is same as in the question
and in page.html
并在 page.html
<script>
$(document).ready(function () {
function qs() {
var qsparam = new Array(10);
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i = 0; i < parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0)
{
var key = parms[i].substring(0, pos);
var val = parms[i].substring(pos + 1);
qsparam[i] = val;
}
}
text1.value = qsparam[0];
text2.value = qsparam[1];
text3.value = qsparam[2];
text4.value = qsparam[3];
}
var splitstr = qs();
});
</script>
<title></title>
</head>
<body>
<input id="text1" type="text" />
<input id="text2" type="text" />
<input id="text3" type="text" />
<input id="text4" type="text" />
</body>
</html>
we are calling the function qs() using
我们正在调用函数 qs() 使用
var splitstr = qs();
var splitstr = qs();
it splits the parameters and to see the value of different variable u can use text1.value=parms if any doubt u can ask :-)
它拆分参数并查看不同变量的值,如果您有任何疑问,您可以使用 text1.value=parms :-)
回答by Mohit Jariwal
sending value like below
发送如下值
window.location.href = "testlocation.aspx?Id=" + tickerCode.label + "&Id2=" + financialYear.label;
and receive like below..
并收到如下所示..
function getActualYear() {
var Id = getParameterByName('Id');
var Id2 = getParameterByName('Id2');
console.log('Id=' + Id);
console.log('Id2=' + Id2);
}
function getParameterByName(id) {
name = name.replace(/[\[]/, "\[").replace(/[\]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
回答by Klas Mellbourn
Here is a detailed answer on how different methods of how to retrieve parameters from the query string with or without jQuery:
这是有关如何使用或不使用 jQuery 从查询字符串中检索参数的不同方法的详细答案:
How can I get query string values in JavaScript?
For example:
例如:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}