添加选项以选择聚焦IE9

adding options to select on focus IE9

本文关键字:聚焦 IE9 选择 选项 添加      更新时间:2023-09-26

我正试图用jquery将选项延迟加载到select中。这段代码适用于我测试过的所有浏览器,IE9除外。(IE7、IE8、FF、Chrome均可使用)

function LazyLoadOptionsIntoSelect($select, options) {
        //get current option
        var selectedOptionVal = $select.val();
        var selectedOptionDisplay = $select.find("option:selected").text();
        //repopulate options
        if (selectedOptionDisplay === "--Select a File--"
                || selectedOptionDisplay === "----------") {
            $select.html("");
            $("<option>").val("").text("--Select a File--")
                        .appendTo($select);
        }
        else {
            $select.html($("option:selected", $select));
            $("<option>").val("").text("----------")
                        .appendTo($select);
        }
        $.each(options, function () {
            var item = this;
            $("<option>").attr("name", function () { return item.display; })
                    .val(item.value)
                    .text(item.display)
                    .appendTo($select);
        });
        //select previous val
        $select.val(selectedOptionVal);
    }
    $(document).on("focus", ".html-select", function () {
        LazyLoadOptionsIntoSelect($(this), HtmlOptions);
    });
    $(document).on("focus", ".txt-select", function () {
        LazyLoadOptionsIntoSelect($(this), TxtOptions);
    });
    $(document).on("focus", ".xml-select", function () {
        LazyLoadOptionsIntoSelect($(this), XmlOptions);
    });

我已经试着解决这个问题好几个小时了,但什么都不起作用。。有什么解决方案吗?或者我需要写一种不同的方式来加载IE9中的选项吗?

options是一个包含值和显示的对象数组。

这适用于更简单的用例,但这显然对微软来说太难处理了&lt_&lt;

经过几个小时的摆弄,我尝试更改css,看看重新绘制元素是否有效。的确如此。

$(document).on("focus", ".html-select", function () {
        LazyLoadOptionsIntoSelect($(this), HtmlOptions);
        if ( $("body").hasClass("ie9") )
        {
            $(this).width($(this).width());
        }
    });