为什么使用 javascript 动态填充选择下拉列表元素在 IE 中不起作用?
我有一个<选择>我在运行时动态填充不同值的元素。
我使用下面的代码来清除
function loadPickerFromObject(pickerControl, values) {
pickerControl.empty();
for (var nameProperty in values) {
if (!$.isFunction(values[nameProperty])) {
var option = $("<option></option>")
.text(values[nameProperty])
.attr("value", nameProperty)
.appendTo(pickerControl);
}
}
}
(我不喜欢编写 javascript,并尝试远离它,如果有更好的方法从对象的属性动态填充
操作)它在 IE 8 中不起作用当我使用 Visual Studio 进行调试时,我可以看到新项目已正确加载,但当我检查页面时,它们没有更新并显示旧项目。
这是怎么回事?它应该显示文本可视化工具窗口中显示的元素(第一个屏幕截图)。为什么它不显示新值?
I have a <select> element that I dynamically populate with different values at runtime.
I use the code below to clear the <option> elements and then reload it with new ones. It works in Firefox 3.6.6 and Chrome 5.0.
function loadPickerFromObject(pickerControl, values) {
pickerControl.empty();
for (var nameProperty in values) {
if (!$.isFunction(values[nameProperty])) {
var option = $("<option></option>")
.text(values[nameProperty])
.attr("value", nameProperty)
.appendTo(pickerControl);
}
}
}
(I'm not fond of writing javascript and try to stay away from it, if there is a better way of dynamically populating a <select> element from the properties of an object please show how)
It doesn't work in IE 8. When I debug using Visual Studio I can see that the new items were loaded correctly, but when I check the page they're not updated and display the old items.
What's up with that? It should display the elements displayed in the Text Visualizer window (first screenshot). Why is it not showing the new values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 add() 方法。所以你的代码看起来像:
You can use the add() method. So your code would look like:
Internet Explorer 喜欢缓存数据。
尝试
CTRL + F5。
并且您可以使用
CTRL + SHIFT + DEL
打开对话框,您可以在其中显式清除缓存。如果这会产生影响,您可能需要考虑添加标头以尝试阻止这种情况发生。 例如:
听起来您是在发出 ajax 数据请求吗?
您还可以尝试:
Internet explorer likes to cache data.
Try
CTRL + F5.
And you can use
CTRL + SHIFT + DEL
to bring up the dialog where you can clear the cache explicitly.If this makes a difference you may want to think about adding headers in to try stop this from happening. For Example:
By the sounds of it your making ajax requests for data?
You can also try:
我认为如果你构建一串 html 并在最后附加到 dom 上,那就更好了。
而不是每次都操作dom。
像这样的东西:
}
i think it would be even better if you will build one string of html and just append to the dom at the end.
instead of manipulating the dom every time.
something like this:
}
GiddyUpHorsey 是正确的,问题出在
removeChild
上。不过,有一种非常简单的方法可以清除
select
。所以你可以这样做:
GiddyUpHorsey is correct, the problem is with
removeChild
.There is a very easy way to clear a
select
though.So you could do this:
我查看了 jQuery
empty()
函数,它在内部调用removeChild
。 IE 似乎无法可靠地在因此,我重写了
loadPickerFromObject
函数,以使用createElement
、add
和remove
函数,而不是 jQuery 的$ ([html])
、appendTo
和empty
函数。我的代码现在可以在 Chrome、Firefox 和 IE 中正常运行。
I took a look at the jQuery
empty()
function and it was callingremoveChild
internally. It seems that IE doesn't work reliably withremoveChild
called on a<select>
element.So I rewrote my
loadPickerFromObject
function to use thecreateElement
,add
andremove
functions instead of jQuery's$([html])
,appendTo
andempty
functions.My code now works properly in Chrome, Firefox and IE.