为什么使用 javascript 动态填充选择下拉列表元素在 IE 中不起作用?

发布于 2024-09-09 08:51:26 字数 936 浏览 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);
        }
    }
}

(我不喜欢编写 javascript,并尝试远离它,如果有更好的方法从对象的属性动态填充

操作)它在 IE 8 中不起作用当我使用 Visual Studio 进行调试时,我可以看到新项目已正确加载,但当我检查页面时,它们没有更新并显示旧项目。

替代文本 alt text

这是怎么回事?它应该显示文本可视化工具窗口中显示的元素(第一个屏幕截图)。为什么它不显示新值?

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.

alt text
alt text

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 技术交流群。

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

发布评论

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

评论(5

嘿哥们儿 2024-09-16 08:51:27

您可以使用 add() 方法。所以你的代码看起来像:

function loadPickerFromObject(pickerControl, values) {
    pickerControl.empty();
    for (var nameProperty in values) {
        if (!$.isFunction(values[nameProperty])) {
            pickerControl.add("<option></option>")
            .text(values[nameProperty])
            .attr("value", nameProperty);
        }
    }
}

You can use the add() method. So your code would look like:

function loadPickerFromObject(pickerControl, values) {
    pickerControl.empty();
    for (var nameProperty in values) {
        if (!$.isFunction(values[nameProperty])) {
            pickerControl.add("<option></option>")
            .text(values[nameProperty])
            .attr("value", nameProperty);
        }
    }
}
ㄟ。诗瑗 2024-09-16 08:51:27

Internet Explorer 喜欢缓存数据。

尝试CTRL + F5。

并且您可以使用CTRL + SHIFT + DEL打开对话框,您可以在其中显式清除缓存。

如果这会产生影响,您可能需要考虑添加标头以尝试阻止这种情况发生。 例如:

<META HTTP-EQUIV="cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

听起来您是在发出 ajax 数据请求吗?
您还可以尝试:

jQuery.ajaxSetup({ cache: false });

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:

<META HTTP-EQUIV="cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

By the sounds of it your making ajax requests for data?
You can also try:

jQuery.ajaxSetup({ cache: false });
烟沫凡尘 2024-09-16 08:51:27

我认为如果你构建一串 html 并在最后附加到 dom 上,那就更好了。
而不是每次都操作dom。
像这样的东西:

function loadPickerFromObject(pickerControl, values) {  
    pickerControl.empty();  
    var optionsHtml;

    for (var nameProperty in values) {  
        if (!$.isFunction(values[nameProperty])) {  
            optionsHtml += "<option value='" + nameProperty  + "'>" + 
                                values[nameProperty] + 
                           "</option>";
        }  
    }

    pickerControl.append(optionsHtml);

}

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:

function loadPickerFromObject(pickerControl, values) {  
    pickerControl.empty();  
    var optionsHtml;

    for (var nameProperty in values) {  
        if (!$.isFunction(values[nameProperty])) {  
            optionsHtml += "<option value='" + nameProperty  + "'>" + 
                                values[nameProperty] + 
                           "</option>";
        }  
    }

    pickerControl.append(optionsHtml);

}

友欢 2024-09-16 08:51:27

GiddyUpHorsey 是正确的,问题出在 removeChild 上。
不过,有一种非常简单的方法可以清除select

mySelect.options.length = 0

所以你可以这样做:

function loadPickerFromObject(pickerControl, values) {
    pickerControl[0].options.length = 0;
    for (var nameProperty in values) {
        if (!$.isFunction(values[nameProperty])) {
            pickerControl.add("<option></option>")
            .text(values[nameProperty])
            .attr("value", nameProperty);
        }
    }
}

GiddyUpHorsey is correct, the problem is with removeChild.
There is a very easy way to clear a select though.

mySelect.options.length = 0

So you could do this:

function loadPickerFromObject(pickerControl, values) {
    pickerControl[0].options.length = 0;
    for (var nameProperty in values) {
        if (!$.isFunction(values[nameProperty])) {
            pickerControl.add("<option></option>")
            .text(values[nameProperty])
            .attr("value", nameProperty);
        }
    }
}
追风人 2024-09-16 08:51:26

我查看了 jQuery empty() 函数,它在内部调用 removeChild。 IE 似乎无法可靠地在

因此,我重写了 loadPickerFromObject 函数,以使用 createElementaddremove 函数,而不是 jQuery 的 $ ([html])appendToempty 函数。

我的代码现在可以在 Chrome、Firefox 和 IE 中正常运行。

I took a look at the jQuery empty() function and it was calling removeChild internally. It seems that IE doesn't work reliably with removeChild called on a <select> element.

So I rewrote my loadPickerFromObject function to use the createElement, add and remove functions instead of jQuery's $([html]), appendTo and empty functions.

My code now works properly in Chrome, Firefox and IE.

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