即使刷新页面(F5或单击浏览器的刷新按钮)后,javascript仍保留隐藏输入变量的值

发布于 2024-12-28 07:26:23 字数 657 浏览 5 评论 0 原文

我面临着一个奇怪的问题,可能你们中的一些人已经遇到过它。 问题是,

我的 html 中有一个隐藏的输入字段(使用 perl /mason ),

<input type='hidden' id='noB'  name='noB' value='<% $noB%>'/> ; 

值($noB)在服务器端填充。

在我的 javascript 中,我用来

var noB = jQuery('#noB').val(); 

获取值并处理它。
最重要的是,每次我将在新变量中的 javascript 函数中访问该值时,假设 noB = 3,进行一些处理并使用新值更新隐藏变量 noB 1,所以它不是我用来做到这一点的全局变量。

现在,如果我更改 noB ,可以在我的 javascript 中设置 1,然后使用 F5 刷新该页面,当页面再次加载时,我会尝试获取值。

var noB = jQuery('#noB').val();

我只得到 noB = 1,而它应该是 3。

请帮忙。

I am facing a weird problem, may be some of you have already faced it.
Here is the issue,

I have a hidden input field in my html ( using perl /mason ),

<input type='hidden' id='noB'  name='noB' value='<% $noB%>'/> ; 

value ($noB) is filled at server side.

In my javascript, I am using

var noB = jQuery('#noB').val(); 

to get value and process it.
Most important thing is everytime I will access this value inside a javascript function in a new variable assume noB = 3, do some processing and update the hidden variable noB with new value as 1, so its not a global variable which I am using to do that.

Now, if I change noB lets say 1 in my javascript and after that page is refreshed using F5, when the page loads again and I try to get value.

var noB = jQuery('#noB').val();

I get noB = 1 only, when It should be 3.

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆悲凉 2025-01-04 07:26:23

Firefox(可能还有其他浏览器)将使用刷新页面时的值重新加载表单元素,即使这不是 HTML 中指定的值。简单的解决方案:通过导航回页面来重新加载页面,而不是刷新。

Firefox (and possibly other browsers) will reload form elements with the values they had when you refreshed the page, even if that's not the value specified in the HTML. Simple solution: reload the page by navigating back to it, rather than refreshing.

回忆那么伤 2025-01-04 07:26:23

虽然它很老,但我遇到了类似的问题并找到了这个解决方案:

  1. 不要使用隐藏的输入字段,而是使用 label.innerHTML 来存储您的值
  2. 您可以使用 javascript 访问/更改标签的innerHTML 值。
  3. 每次刷新页面时,标签的值都会刷新。

希望有帮助。

Although its very old one but I ran into a similar problem and found this solution:

  1. Instead of using hidden input field, use a label.innerHTML to store your value
  2. You can access/alter label's innerHTML value using javascript.
  3. Label's value will get refreshed each time the page is refreshed.

Hope it helps.

岁吢 2025-01-04 07:26:23

如果这是浏览器问题,请尝试强制浏览器不缓存您的页面内容。

这可以通过将以下 标记放入 HTML 标头中来完成:

    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">

If this is a browser issue, try forcing the browsers not to cache your page content.

This can be done by putting the following <meta> tag inside your HTML header:

    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文