javascript 为什么 Chrome 在使用后退按钮后不显示选定的 <option> 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10333973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Chrome not show a selected <option> element after using the back button?
提问by Intru
I have a <select>
tag that contains a single <option>
element:
我有一个<select>
包含单个<option>
元素的标签:
<select id="someselect">
<option value="2">B</option>
</select>
The single <option>
is later replaced using jQuery. The new list of options always contains the old option:
单曲<option>
后来被 jQuery 取代。新选项列表始终包含旧选项:
selected = $('#someselect').val();
$('#someselect').html('<option value="1">A</option><option value="2">B</option>').val(selected);
This works as expected. However, when navigating away from the webpage in Google Chrome and then clicking the back button, something weird happens. The select tag is back to its initial state (makes sense), but the single <option>
element is not selected!
这按预期工作。但是,当离开 Google Chrome 中的网页然后单击后退按钮时,会发生一些奇怪的事情。select 标签恢复到它的初始状态(有道理),但<option>
没有选择单个元素!
What is the cause of this behavior in Chrome?
Chrome 中这种行为的原因是什么?
I've created a minimal working example: http://dl.dropbox.com/u/27566470/backdemo.html
Initially there is only a single <option>
. First click on "click" to replace the options (but keep the 'B' option selected), then click on "Google" to navigate away, and then use the back button of Chrome to see the <select>
tag with only a single option that is not selected.
我创建了一个最小的工作示例:http: //dl.dropbox.com/u/27566470/backdemo.html
最初只有一个<option>
. 首先点击“点击”替换选项(但保持选择“B”选项),然后点击“谷歌”导航离开,然后使用Chrome的后退按钮查看<select>
只有一个选项的标签未选中的。
Edit: to clarify, I'm not interested in how to fix this. I'm curious why Chrome works like this. Serving the original (unmodified) DOM after using the back button makes sense, but why is the only select option not selected?
编辑:澄清一下,我对如何解决这个问题不感兴趣。我很好奇为什么 Chrome 会这样工作。使用后退按钮后提供原始(未修改的)DOM 是有道理的,但为什么没有选择唯一的选择选项?
采纳答案by Intru
To answer my own question, it seems to be a Chromium bug. I've filed a bug that is currently confirmed and triaged: http://code.google.com/p/chromium/issues/detail?id=125509
So hopefully this should be fixed in the future :)
回答我自己的问题,这似乎是一个 Chromium 错误。我已经提交了一个目前已确认和分类的错误:http: //code.google.com/p/chromium/issues/detail?id= 125509
所以希望这应该在未来得到修复:)
回答by Zaheer Ahmed
Because when browser back button is used it use the cache version of page but in your page there is dynamic data which got lost so browser does not find the previously selected value, You can set it by using jQuery:
因为当使用浏览器后退按钮时,它使用页面的缓存版本,但在您的页面中有动态数据丢失,因此浏览器找不到先前选择的值,您可以使用 jQuery 设置它:
$(document).ready(function(){
$('#someselect').val('0');
});
回答by David Diez
Because every browser that navigates outside the document (# is still inside) when navigates back they load the server cached version, and that said Chrome modifies the DOM when you select an option taking away the selected
attribute to the selected option element and when you get back it loads again with the selected
因为每个浏览器在文档外部(# 仍然在内部)导航时,它们会加载服务器缓存的版本,并且当您选择一个选项时,Chrome 会修改 DOM 将selected
属性移至所选选项元素,当您返回时它再次加载selected