添加选项以选择焦点IE9

时间:2012-01-23 23:06:43

标签: javascript jquery html internet-explorer internet-explorer-9

我尝试将选项延迟加载到带有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是一个包含value和display的对象数组。

这适用于更简单的用例,但这对微软来说显然太过分了。 &LT; _&LT;

1 个答案:

答案 0 :(得分:1)

经过数小时的摆弄,我尝试更改css以查看重绘元素是否有效。它确实。

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