使用 jQuery/Javascript 保留 Django 中显示/隐藏部分的状态
我有一个带有一些显示/隐藏部分的表单。当我按下主窗体中的某些按钮时,会显示不同的窗体。编辑这些表单中的信息后,我将返回到第一个表单。但显示/隐藏部分的状态已重置。显示/隐藏操作是通过 jquery 切换完成的。返回时需要保持这些部分的状态。 所以我正在考虑将每个部分的状态存储在会话变量中。您还有其他解决方案吗?将这种行为放在单独的地方(例如在中间件中)会更好还是可能?
I have a form with some show/hide sections. When I press some buttons in my home form, different forms will be shown. After I edit the information in those forms I'm returning to the first form. But the state of the show/hide sections is reseted. The show/hide action is done with jquery toggle. The state of these sections needs to be kept on returning.
So I'm thinking of storing the state of each section in session variables. Do you have other solutions? Would it be better or possible to have this behavior in a separate place, for example in a middleware?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上并不需要会话存储。在您所描述的情况下,您希望为单个客户端在页面之间保留部分的显示/隐藏状态。会话可能有点过分了,因为它涉及到服务器一些琐碎的事情。
我的做法是直接在 jQuery 中使用 localStorage(或者 cookies,如果你真的想要的话)之类的东西。它将保留在客户端上,服务器不会介意。
编辑
检查 localStorage 的函数:
您需要更改切换代码来设置值:
在 init 函数中:
You don't really need session storage. In the case you've described, you want the sections' show/hide status to be kept between pages, for a single client. Session would be overkill, as it involves the server for something trivial.
My go would be to use something like localStorage (or cookies if you really want) directly in the jQuery. It will stay on the client and the server will not mind.
EDIT
Function to check for localStorage:
You would need to change the toggling code to set a value:
And in init function: