javascript - <script> 的 innerHTML 适用于 FF,不适用于 IE

标签 javascript jquery internet-explorer firefox

我正在尝试通过 Ajax 填充 <select> 元素。它在 FF 中运行良好,但我在 IE 中得到一个 unknown runtime error

HTML:

<select id="emp_select" name="emp_select">
    <option value=" ">Please enter a store</option> 
</select>

Javascript:

$("#store").blur(function() {
    populateDropdowns();
});

...

function populateDropdowns() {
        var store = $("#store").val();

        if (store.length != 4) {
            alert("Store # must be 4 digits!");
            $("#store").focus();
            return false;
        }

        var xhrJSON = new XMLHttpRequest();
        var xhrEmpSelect = new XMLHttpRequest();
        var xhrMgrSelect = new XMLHttpRequest();

        var jsonDone = false;
        var empSelectDone = false;
        var mgrSelectDone = false;

        $("#processing-dialog").dialog({
                width: 300,
                height: 150
        });

        xhrJSON.onreadystatechange = function() {
            if (xhrJSON.readyState == 4) {
                if (xhrJSON.status == 200) {
                    var js = document.createElement('script');
                    js.type = 'text/javascript';

                    js.innerHTML = xhrJSON.responseText;
                    var scr = document.getElementsByTagName('script')[1];
                    scr.parentNode.insertBefore(js,scr);

                    jsonDone = true;
                    if (jsonDone && empSelectDone && mgrSelectDone) {
                        $("#processing-dialog").dialog("close");
                        $("#processing-dialog").dialog("destroy");
                        return true;
                    }
                } else {
                    return false;
                }
            }
        }

        xhrEmpSelect.onreadystatechange = function() {
            if (xhrEmpSelect.readyState == 4) {
                if (xhrEmpSelect.status == 200) {
                    $("#emp_select").html(xhrEmpSelect.responseText);
                    empSelectDone = true;
                    if (jsonDone && empSelectDone && mgrSelectDone) {
                        $("#processing-dialog").dialog("close");
                        $("#processing-dialog").dialog("destroy");
                        return true;
                    }
                } else {
                    return false;
                }
            }
        }

        xhrMgrSelect.onreadystatechange = function() {
            if (xhrMgrSelect.readyState == 4) {
                if (xhrMgrSelect.status == 200) {
                    $("#mgr_select").html(xhrMgrSelect.responseText);
                    mgrSelectDone = true;
                    if (jsonDone && empSelectDone && mgrSelectDone) {
                        $("#processing-dialog").dialog("close");
                        $("#processing-dialog").dialog("destroy");
                        return true;
                    }
                } else {
                    return false;
                }
            }
        }

        var url = "ajax.cgi";

        var JSONdata = "action=generateJSON&store=" + store;
        var EmpSelectData = "action=generateEmpSelect&store=" + store;
        var MgrSelectData = "action=generateMgrSelect&store=" + store;

        xhrJSON.open("POST",url);
        xhrJSON.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhrJSON.send(JSONdata);

        xhrEmpSelect.open("POST",url);
        xhrEmpSelect.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhrEmpSelect.send(EmpSelectData);

        xhrMgrSelect.open("POST",url);
        xhrMgrSelect.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhrMgrSelect.send(MgrSelectData);
    }

blur 处理程序调用一个函数来填充(所有)不同的选择元素,加上一个 JavaScript 对象,该对象包含所有员工的关联数组,以将名称与作为选项值返回的员工 ID 匹配在两个 select 元素中。

返回的 XHR 文本(对于 xhrJSON,content-type=application/json):

var empArray = new Array({ id:"12345678", title:"The Title", code:"C123", name:"John Doe"},...);

为 (xhrEmpSelect, content-type=text/html) 返回的 XHR 文本:

<option value=" ">Select One</option>
<option value="John Doe">John Doe</option>
<option value="Joe Blow">Joe Blow</option>
...
<option value="other">Other...</option>
</select>

xhrMgrSelect 返回相似文本,content-type=text/html

因此在 FF 中一切正常,JS 对象出现并插入到 DOM 中,并且两个 <select> 元素也被填充。但是在 IE 中,我在 unknown runtime error 处理程序中得到了一个 xhrJSON.onreadystatechange,我尝试将 js.innerHTML 设置为 xhrJSON.responseText

我做错了什么?

最佳答案

尝试使用 js.text = xhrJSON.responseText;而不是 innerHTML .我相信您遇到的问题与您无法将 HTML 插入到 <script> 中有关。 block 。

关于javascript - &lt;script&gt; 的 innerHTML 适用于 FF,不适用于 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775109/

相关文章:

jquery - IE<v9问题,标签中的jquery UI单选按钮img不会触发值更改

javascript - 是否可以仅使用图像运行 javascript

javascript - 根据显示位置动态更改弹出窗口的位置

jquery - 将列表元素拉伸(stretch)到整个容器宽度

javascript - 使用 DOM 元素的 id 访问插件实例

jQuery GET 不发送浏览器中的 IE

javascript - 从手机上的 OneDrive 中选择一个文件 - 移动网站

javascript - 从 select2 选中的选项中获取属性值

javascript - 仅当输入字段为空时才将文本发送到输入字段

html - 图片未加载,IE6 到 IE9