我想在winRT应用程序中使用jQuery将jQuery从一个页面传递到另一个页面。 使用一个参数完成。如何传递多个参数并在第二页中检索?
编码一个参数。
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>
答案 0 :(得分:7)
您可以使用“?param = foo”为第一个参数将GET参数传递给页面。附加参数包含在“&amp;”中而不是“?”。
由于您传递了GET参数,因此您可以将它们全部放入URL中并在目标页面中读取它们。或者,您可以使用POST参数。
以下是GET与PUT参数的参考:
http://www.w3schools.com/tags/ref_httpmethods.asp
如果您将所有参数作为GET传递,则可以展开此位:
var te = $("#text1").val();
var tea = $("#text2").val();
window.location.href = "/page.html?cal=" + te;
这样的事情:
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;
至于获取其他页面中的参数:我假设你想通过jQuery获取其他页面中的GET参数,因为你的目标页面是纯HTML。如果是这种情况,请检查this question
答案 1 :(得分:4)
使用这种技术完成 在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
并在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>
我们使用
调用函数qs()var splitstr = qs();
它拆分参数并查看不同变量的值你可以使用text1.value = parms 如果有任何疑问你可以问: - )
答案 2 :(得分:0)
以下是关于如何使用或不使用jQuery从查询字符串中检索参数的不同方法的详细解答:
How can I get query string values in JavaScript?
例如:
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, " "));
}
答案 3 :(得分:0)
发送如下的值
window.location.href = "testlocation.aspx?Id=" + tickerCode.label + "&Id2=" + financialYear.label;
并收到以下内容..
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, " "));
}