Chrome 不会缓存隐藏表单字段值以供在浏览器历史记录中使用
我有一个 ASP.Net Web 表单,其中包含文本框字段和隐藏字段。使用客户端 JavaScript 动态修改隐藏字段值。发布表单、检查值并重定向到另一个页面都按预期工作。
但是,当我使用浏览器后退按钮显示上一页时,我希望看到所有表单字段仍然填充有发布的值。
在 IE 和 Firefox 中,文本和隐藏输入字段都是这种情况。
在 Chrome 中,这仅适用于文本字段。隐藏字段的值丢失了。
在浏览浏览器历史记录时,Chrome 是否永远不会重新填充动态设置隐藏的表单字段值?
我已经整理了一个小样本来演示该问题,并可以在需要时提供。我想首先问这个问题,看看这是否是众所周知的行为以及我必须接受的行为。
I have a ASP.Net web form that contains both text box fields and hidden fields. The hidden field values are modified dynamically using client side JavaScript. Posting the form, inspecting the values and redirecting to another page is all working as expected.
However, when I use the browser back button to display the previous page then I expect so see that ALL form fields are still populated with the values that were posted.
In IE and Firefox this is the case for both text and hidden input fields.
In Chrome this is ONLY the case for text fields. The value of hidden fields is lost.
Is it true that Chrome never repopulates dynamically set hidden form field values when navigating the browser history?
I have put a small sample together to demonstrate the problem and can provide that if required. I wanted to first ask the question to see if this is well known behaviour and something I have to accept.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题可以通过一个小技巧来解决。
问题是 Chrome 浏览器无法正确处理 Type=hidden 和动态设置值的表单字段。
因此,解决方案是将字段的类型更改为文本,并使用其他方法隐藏可见的文本框。这可以通过包围所有带有 DEV 标签对要隐藏的值的文本框并将样式指定为 display: none 来实现。
然后在页面上您将不会看到带有隐藏值的文本框,并且它将与 JavaScript 一起正常工作浏览器。
之前
之后
This problem could be solved using a small trick.
The problem is Form fields with Type=hidden with Dynamically set values are not handled properly by the Chrome Browser.
So the solution is to change the type of the field to text and use some other method to hide the visible text boxes. This could be achieved by surrounding all the text boxes carrying values intended to be hidden by a DEV tag pair and assigning the style as display: none
Then on the page you wont see the text boxes carrying hidden values and it will work properly with JavaScript of the browser.
BEFORE
AFTER
您不应该依赖这种行为。不同的浏览器甚至不同的浏览器版本都有所不同。任何标准中均未描述此行为。如果您希望字段具有特定值,您可以使用 cookie,或者在页面加载时始终向服务器发出请求,或者使用更现代的方法,例如本地存储(尽管它没有得到广泛支持)。
You should not rely on this behavior. It is different among browsers, even among browser versions. This behavior is not described in any standards. If you want your fields to have specific values, you can use cookies, or always make requests to the server when page loads, or use more modern methods like local storage (it is not widely supported though).
我似乎无法发表评论,也许我的代表太低,但觉得这一点很重要。
我自己刚刚在 Opera 中遇到了这个问题,因此借用了 Sanesh Fernando 的解决方案,该解决方案解决了隐藏字段未恢复的问题(感谢 Sanesh)。然而,给我带来的问题是,Javascript 在表单字段更新之前触发,因此如果您像我一样使用 javascript 检查值,那么我必须添加一个 setTimeout 以确保 Opera 在检查值之前更新。
Cookie 是以另一种方式表述的,但是对于要求访问者同意 Cookie 使用的荒谬的欧盟指令,这对我来说不是一个解决方案。
I can't seem to comment, maybe my rep too low but felt this is important to mention.
I just ran into this problem myself in Opera so borrowed Sanesh Fernando's solution which worked around the hidden fields not being reinstated (Thanks Sanesh). However what caused a problem for me was that Javascript fires before the form fields are updated so if you check values with javascript as I was doing then I had to add a setTimeout to ensure Opera updated before I checked the values.
Cookies are as stated another way but what with the ridiculous EU directive on requiring cookie usage agreement from the visitor it's not a solution for me.