如何在 jQuery 中的会话中获取价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15510590/
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 get value in the session in jQuery
提问by Sharath Kumar
I'm a beginner in jQuery.
I want to set the values in an HTML page and I have to get them in another HTML page.
我是 jQuery 的初学者。
我想在一个 HTML 页面中设置值,我必须在另一个 HTML 页面中获取它们。
Here is the piece of code I am trying now:
这是我现在正在尝试的一段代码:
To set the value in session:
要在会话中设置值:
$.session.set(userName, $("#uname").val());
To get the value from session:
从会话中获取值:
$.session.get('userName');
回答by Asad Saeeduddin
Assuming you are using this plugin, you are misusing the .set
method. .set
must be passed the name of the key as a string as well as the value. I suppose you meant to write:
假设您正在使用此插件,则您正在滥用该.set
方法。.set
必须将键的名称作为字符串以及值传递。我想你的意思是写:
$.session.set("userName", $("#uname").val());
This sets the userName
key in session storage to the value of the input, and allows you to retrieve it using:
这userName
会将会话存储中的键设置为输入的值,并允许您使用以下方法检索它:
$.session.get('userName');
回答by Interwebtual
Sessions are stored on the server and are set from server side code, not client side code such as JavaScript.
会话存储在服务器上,并从服务器端代码设置,而不是客户端代码(如 JavaScript)。
What you want is a cookie, someone's given a brilliant explanation in this Stack Overflow question here: How do I set/unset cookie with jQuery?
你想要的是一个 cookie,有人在这个 Stack Overflow 问题中给出了一个精彩的解释: How do I set/unset cookie with jQuery?
You could potentially use sessions and set/retrieve them with jQuery and AJAX, but it's complete overkill if Cookies will do the trick.
您可以潜在地使用会话并使用 jQuery 和 AJAX 设置/检索它们,但是如果 Cookies 能够做到这一点,那就完全是矫枉过正了。