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

时间:2022-09-07 10:50:32

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 ),

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

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

value ($noB) is filled at server side.

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

In my javascript, I am using

在我的javascript中,我正在使用

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.

获得价值并加以处理。最重要的是每次我都会在新变量中的javascript函数中访问这个值,假设noB = 3,做一些处理并用新值更新隐藏变量noB为1,所以它不是我用来做的全局变量那。

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.

现在,如果我更改noB,请在我的javascript中说1,然后使用F5刷新页面,当页面再次加载并尝试获取值时。

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

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

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

Please help.

请帮忙。

3 个解决方案

#1


2  

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.

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

#2


0  

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. 不使用隐藏的输入字段,而是使用label.innerHTML来存储您的值
  3. You can access/alter label's innerHTML value using javascript.
  4. 您可以使用javascript访问/更改label的innerHTML值。
  5. Label's value will get refreshed each time the page is refreshed.
  6. 每次刷新页面时,Label的值都会刷新。

Hope it helps.

希望能帮助到你。

#3


0  

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:

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

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

#1


2  

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.

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

#2


0  

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. 不使用隐藏的输入字段,而是使用label.innerHTML来存储您的值
  3. You can access/alter label's innerHTML value using javascript.
  4. 您可以使用javascript访问/更改label的innerHTML值。
  5. Label's value will get refreshed each time the page is refreshed.
  6. 每次刷新页面时,Label的值都会刷新。

Hope it helps.

希望能帮助到你。

#3


0  

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:

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

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