简体   繁体   English

在用户交互之间保存 Javascript 变量?

[英]Save a Javascript variable between user interactions?

Setting global variables in my js file doesn't seem to work.在我的 js 文件中设置全局变量似乎不起作用。 How can I set rails session variables from JavaScript?如何从 JavaScript 设置 rails session变量?

I'd like a solution that doesn't use jQuery.我想要一个不使用 jQuery 的解决方案。


Edit : The solution is to use an HTML form containing hidden fields.编辑:解决方案是使用包含隐藏字段的 HTML 表单。

Global variables only live for the duration of a page view.全局变量仅在页面查看期间有效。 Once the page is rerendered, they are reset to what their default values are.页面重新呈现后,它们将重置为默认值。

If you need to keep the values, you need to use cookies or local storage.如果需要保留这些值,则需要使用 cookie 或本地存储。 Other option is to submit them to the server with Ajax and have the server remember them and set the values when the page is rendered.另一种选择是使用 Ajax 将它们提交到服务器,并让服务器记住它们并在呈现页面时设置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM