IE8奇怪的行为动态下拉列表

时间:2013-12-04 14:52:00

标签: javascript jquery html drop-down-menu internet-explorer-8

感谢您查看此信息。在我将一个值(即“51”)放入特定页面的javascript后,我得到了这个动态ddl。因此,不同的页面根据数值拉出不同的列表。一切都很好,除了IE 8(押韵+1)。当我在IE8中打开时,我只得到一个下拉项目,上面写着“选择一个程序优先”。

我做了一些研究,发现有一篇文章在这里谈论.empty对IE8不可靠,但我不使用它。这是我的工作代码:

本地代码:

<script type="text/javascript" language="javascript">
<!--
   $(document).ready(function () {
       $('#ballform_program').val(51);
       loadCampuses();
   });
//-->
    </script>

引用的DDL功能js:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<!-- Populate Campus DDL -->
<script type="text/javascript" language="javascript">
<!--
    $(document).ready(function () {
        if (firstField("#ballform_campuslocation", "#ballform_program") == "#ballform_campuslocation") {
            loadCampuses(); 
            $('#ballform_campuslocation').change(loadPrograms);
        } else if (firstField("#ballform_campuslocation", "#ballform_program") == "#ballform_program") {
            loadPrograms();
            $('#ballform_program').change(loadCampuses);
        }

        $('#ballform_searchterm').val(getSearch());
        $('#ballform_url').val(location.href);
        $('#ballform_referrer').val(document.referrer);
        $('#ballform_useragent').val(navigator.userAgent);

        $.getJSON("http://jsonip.appspot.com?callback=?",
            function(data) {
                $('#ballform_ipaddress').val(data.ip);
        });
    });

    function loadCampuses() {
        var host = location.host;
        var data;

        if (firstField("#ballform_campuslocation", "#ballform_program") == "#ballform_campuslocation") {
            data = liveballScriptlet(6, "json", "url=" + host + "&program=0");
            clearProgram();
        } else {
            var programID = $('#ballform_program').val();
            data = liveballScriptlet(6, "json", "url=" + host + "&program=" + programID);
        }

        var options = "<option value=''>Choose...</option>";
        var optGroup = "";
        var jsonString = $.parseJSON(data);

        $.each(jsonString, function () {
            var value = this['ID'];
            var text = this['Name'];
            var school = this['School'];

            if (optGroup != school) {
                if (optGroup != "") {
                    options += "</optgroup>";
                }
                optGroup = school;
                options += "<optgroup label='" + optGroup + "'>";
            }
            options += "<option value='" + value + "'>" + text + "</option>";
        });
        $('#ballform_campuslocation').html(options);
    }

    function clearCampus() {
        if ($('#ballform_campuslocation').length > 0) {
            $('#ballform_campuslocation')
                .find('option')
                .remove()
                .end()
                .append('<option value="">Choose a Program First</option>');

            $('#ballform_campuslocation')
                .find('optgroup')
                .remove()
                .end();
        }
    }

    function loadPrograms() {
        var host = location.host;
        var data;

        if (firstField("#ballform_campuslocation", "#ballform_program") == "#ballform_program") {
            data = liveballScriptlet(7, "json", "url=" + host + "&campus=0");
            clearCampus();
        } else {
            var campusID = $('#ballform_campuslocation').val();
            data = liveballScriptlet(7, "json", "url=" + host + "&campus=" + campusID);
        }

        var options = "<option value=''>Choose...</option>";
        var optGroup = "";
        var jsonString = $.parseJSON(data);

        $.each(jsonString, function () {
            var value = this['ID'];
            var text = this['Name'];
            var degree = this['Degree'];

            if (optGroup != degree) {
                if (optGroup != "") {
                    options += "</optgroup>";
                }
                optGroup = degree;
                options += "<optgroup label='" + optGroup + "'>";
            }
            options += "<option value='" + value + "'>" + text + "</option>";
        });
        $('#ballform_program').html(options);
    }

    function clearProgram() {
        if ($('#ballform_program').length > 0) {
            $('#ballform_program')
                .find('option')
                .remove()
                .end()
                .append('<option value="">Choose a Campus First</option>');

            $('#ballform_program')
                .find('optgroup')
                .remove()
                .end();
        }
    }

    function getSearch() {
        var work = "", output = "";
        var start = 0, length = 0;

        work = document.referrer;
        start = work.indexOf("q=") + 2;

        work = work.substring(start);
        length = work.indexOf("&");

        if (length == -1) {
            output = work;
        } else {
            output = work.substring(0, length);
        }

        return output;
    }

    function firstField(object1, object2) {
        var o1Exists, o1, o1Pos;
        var o2Exists, o2, o2Pos;
        var value = ""; 

        //check for existence
        o1Exists = ($(object1).length);
        o2Exists = ($(object2).length);

        if (!o1Exists && !o2Exists) {
            value = "";
        } else if (o1Exists && !o2Exists) {
            value = object1;
        } else if (!o1Exists && o2Exists) {
            value = object2;
        } else {
            try {
                o1 = $(object1);
                o2 = $(object2);

                o1Pos = o1.position();
                o2Pos = o2.position();

                if (o1Pos.top < o2Pos.top) {
                    value = object1;
                } else {
                    value = object2;
                }
            } catch (err) {
                value = object1;
                alert(err);
            }
        }

        return value;
    }

//-->
</script>

任何人都有修复可能性,不涉及重写整个“工作”脚本?非常感谢!

1 个答案:

答案 0 :(得分:0)

@epascarello在评论中指出我可以尝试交换:

$('#ballform_campuslocation').html(options); 

使用:

$('#ballform_campuslocation').append(options);

并且,对于这种情况,它工作了!再次感谢。