Javascript 如何使用 jQuery 在 Session 中存储值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5263207/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:23:52  来源:igfitidea点击:

How to store the value in Session using jQuery?

javascriptjquery

提问by kumar

How to store this value in session?

如何在会话中存储这个值?

My grid has 3 pages.. with page size 50 when I change this dropdown list box in first page it's doing perfect. but when I got to second page I am not seeing selected value for this dropdown list boxes?

我的网格有 3 页 .. 页面大小为 50 当我在第一页中更改此下拉列表框时,它做得很完美。但是当我进入第二页时,我没有看到此下拉列表框的选定值?

Is there any way that we can maintain seession for this dropdownlist box value to send to the next page to set?

有什么方法可以让我们保持这个下拉列表框值的发送到下一页设置?

回答by Konrad Garus

jQuery operates on client side, while session is a server-side construct. You'll need to check with your HTTP server (whatever technology you're using) for it.

jQuery 在客户端运行,而 session 是服务器端构造。您需要检查您的 HTTP 服务器(无论您使用什么技术)。

If you aren't posting it to the server, you can use cookies for persistent client-side storage. Or, if you're not reloading the page, you can store it in regular javascript variables.

如果您不将其发布到服务器,则可以使用 cookie 进行持久的客户端存储。或者,如果您不重新加载页面,则可以将其存储在常规 javascript 变量中。

回答by Pointy

There's no way to have jQuery store something in your session context. The session is a server-side concept, and is represented at the client by nothing more than an identifier.

没有办法让 jQuery 在你的会话上下文中存储一些东西。会话是服务器端的概念,在客户端仅由标识符表示。

You'll have to have a server action you can POST to, and that action can put parameters sent with the POST into the session.

您必须有一个可以 POST 到的服务器操作,并且该操作可以将随 POST 发送的参数放入会话中。

From the client, you also won't be able to accessthe contents of the session unlessyour pages explicitly dump out some sort of map onto the page.

从客户端,您也将无法访问会话的内容,除非您的页面显式地将某种映射转储到页面上。

回答by Michael Jasper

By session, do you mean your server-side session variable? If so, you would need to send a post to a page of your language of choice, parse out the variable, and then on the next page, read the session variable and set the option selection accordingly.

通过会话,您的意思是您的服务器端会话变量吗?如果是这样,您需要将帖子发送到您选择的语言的页面,解析出变量,然后在下一页上读取会话变量并相应地设置选项选择。

If by session, you mean keeping the same option on the client side, this could easily be accomplished by setting a cookie on the clients machine through javascript and then read it in on the next page, or to send a query string along with the url of the next page.

如果通过会话,您的意思是在客户端保留相同的选项,这可以通过在客户端机器上通过 javascript 设置 cookie 然后在下一页读取它,或者发送查询字符串和 url 来轻松完成下一页。

EDIT

编辑

If there is no automatic post, I would recommend setting a cookie when a option is chosen.

如果没有自动发布,我建议在选择选项时设置 cookie。

Here is some pseudocodeyou can adapt to work for you: (more on javascript cookies @ w3schools)

以下是一些您可以适应的伪代码:(更多关于 javascript cookies @ w3schools

<option onclick="setcookie("#id")">
function setCookie(choice){
  document.cookie="option=" + choice;
}

next page:

下一页:

$(document).load(checkCookie());
function getCookie(option)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==option)
    {
    return unescape(y);
    }
  }
}
function checkCookie()
{
var option=getCookie("option");
  if (option!=null && option!="")
  {
   $(option).selected();
  }
}

回答by JAAulde

You can use cookies. This cookies libraryhas jQuery bindings to cause form elements to be auto filled based on cookie values, and vice versa.

您可以使用 cookie。这个cookie 库具有 jQuery 绑定,可以根据 cookie 值自动填充表单元素,反之亦然。

Otherwise, you need to follow the advice of Konrad Garus, Pointy, and Michael Jasper--go to server side.

否则,您需要遵循 Konrad Garus、Pointy 和 Michael Jasper 的建议——转到服务器端。

回答by Theon Lin

Another way is using the Cookie. jQuery have cookie plug-in(http://plugins.jquery.com/project/Cookie). You could store the value in the cookie, and get them in the next page. But, it won't work if user turn off cookie.

另一种方法是使用 Cookie。jQuery 有 cookie 插件(http://plugins.jquery.com/project/Cookie)。您可以将值存储在 cookie 中,然后在下一页中获取它们。但是,如果用户关闭 cookie,它将不起作用。