jQuery 如何在jQuery中将值从一个页面传递到另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/404891/
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
How to pass values from one page to another in jQuery
提问by venkatachalam
I have two pages of jQuery, Page1 and Page2, and I'm able to get input in Page1.
我有两页 jQuery,Page1 和 Page2,我可以在 Page1 中获取输入。
The somval=1000$
.
的somval=1000$
。
The page 1 user enters the somevalue. I have stored the value:
第 1 页的用户输入某个值。我已经存储了值:
var val = somval;
Now in the second page, I need to get the result of somvalue in page 1. Of course two pages using My1.js My2.js respectively.
现在在第二页,我需要得到第1页somvalue的结果。当然两个页面分别使用My1.js和My2.js。
How do I share the values from one jQuery file to other JavaScript or how do I get the value from page1 value, to page2?
如何将一个 jQuery 文件中的值共享到其他 JavaScript,或者如何将值从 page1 值获取到 page2?
How do I tackle this?
我该如何解决这个问题?
回答by gak
You can redirect the user to the next page with the data in a query string, then in the second page you can parse the URL.
您可以使用查询字符串中的数据将用户重定向到下一页,然后在第二页中您可以解析 URL。
To redirect the user you can do this:
要重定向用户,您可以执行以下操作:
window.location = 'page2.html?somval=' + somval;
Then in the second page you can use a function to parse the URL query string:
然后在第二页中,您可以使用一个函数来解析 URL 查询字符串:
var qsParm = new Array();
function qs() {
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);
qsParm[key] = val;
}
}
}
This code was borrowed from here, although there are many ways to do it.
You mentioned jQuery, but correct me if I'm wrong, I don't think it can do this.
你提到了 jQuery,但如果我错了,请纠正我,我认为它不能做到这一点。
回答by stuartloxton
You could set the data in a cookie, http://www.quirksmode.org/js/cookies.htmlhas functions for reading and writing cookies.
您可以在 cookie 中设置数据,http://www.quirksmode.org/js/cookies.html具有读取和写入 cookie 的功能。
回答by PhiLho
The problem isn't specific to jQuery (ie. it probably haven't a specific solution, unless I am mistaken), but to any Web page.
Gerald gave the URL query string solution, the other classical solution, if you want to be independent of a server, is to use cookies, they are made for that!
Using a server, you have session data, hidden fields, Ajax save and restore, etc.
问题不是特定于 jQuery(即它可能没有特定的解决方案,除非我弄错了),而是任何网页。
Gerald 给出了 URL 查询字符串的解决方案,另一个经典的解决方案,如果你想独立于一个服务器,就是使用 cookie,它们就是为此而生的!
使用服务器,您可以获得会话数据、隐藏字段、Ajax 保存和恢复等。