IE 9没有显示由js填充的表

时间:2014-06-24 15:21:25

标签: javascript html

我遇到了' addedItems'表格不在IE 9中显示,但如果在Firefox 30上使用则显示它应该显示。在IE中,它发送帖子数据,当我用开发人员工具检查HTML时,它会在页面上显示html元素。我试图看看是否将表格显示设置为阻止但没有变化。

此代码的目的是允许用户从下拉列表中选择他们想要添加的设备类型,然后能够将Foo或Bar列表中的任意数量的项目添加到表格中发送项目[]发布变量。该表还为每一行都有一个删除按钮,以便用户可以取出错误添加的设备。

这是HTML文件:

<html>  
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    </head>
    <body>
    <form method="post">
        <ul>
            <li id="EquipmentSelector" >
                <label for="EquipmentType">Equipment Type</label>
            <div>
                <select id="EquipmentType" name="EquipmentType">

                    <option value="" selected="selected"></option>
                    <option value="0" >Foo</option>
                    <option value="1" >Bar</option>

                </select>
            </div>

        </li>
        <li id = "FooHolder">

            <label class="description" for="Foo">Foo</label>


                <select id="Foo">

                    <option value="" selected="selected"></option>

                        <option value="0" >Foo Zero</option>
                        <option value="1" >Foo One</option>
                        <option value="2" >Foo Two</option>
                        <option value="3" >Foo Three</option>

                </select>




            <input type = "button" value = "Add Foo" id = "FooClick" ></input>

        </li>
        <li id = "BarHolder">

            <label class="description" for="Bar">Bar</label>

                <select id="Bar">

                    <option value="" selected="selected"></option>

                    <option value="0" >Bar Zero</option>
                    <option value="1" >Bar One</option>
                    <option value="2" >Bar Two</option>
                    <option value="3" >Bar Three</option>

                </select>


            <input type = "button" value = "Add Bar" id = "BarClick" ></input>

        </li>

        <li>
        <table>
                    <tbody  id = "addedItems">
                    </tbody>    
        </table>
        </li>
        <li>

            <input type= "submit" id="saveForm" name="submit" value="Submit"  />

        </li>
    </ul>

    </form>

    <script src = "IEerror.js"></script>
    </body>
</html>

这是IEerror.js文件:

function prepareEventHandlers(){

    document.getElementById("EquipmentType").onchange = function(){
        var equipType = document.getElementById("EquipmentType").value
        if(equipType === "1"){
            document.getElementById("BarHolder").style.display = "block";
            document.getElementById("FooHolder").style.display = "none";
        } else if(equipType === "0"){
            document.getElementById("FooHolder").style.display = "block";
            document.getElementById("BarHolder").style.display = "none";
        } 
    }

    document.getElementById("FooHolder").style.display = "none";
    document.getElementById("BarHolder").style.display = "none";


    function removeDiv() {
        var parentId = $(this).parent().parent().attr("id");
        $('#' + parentId).remove();
    }


    var myNum = 0;
    function addItem(getFromId){
        var addTag = document.getElementById("addedItems");
        var addVariable = document.getElementById(getFromId).value;
        var possibleId = getFromId + addVariable;
        if(!document.getElementById(possibleId)){
            var newTr = document.createElement("tr");
            newTr.id = possibleId;
            var myText = $('#'+ getFromId).find(":selected").text();
            var newTd = document.createElement("td");
            newTd.appendChild(document.createTextNode(myText));
            newTr.appendChild(newTd);
            addTag.appendChild(newTr);

            var submissionInput = document.createElement("input");
            submissionInput.name = "item[]";
            submissionInput.type = "hidden";
            submissionInput.value = myText;
            newTd.appendChild(submissionInput);

            var deleteInput = document.createElement("input");
            deleteInput.type = "button";
            deleteInput.value = "Delete";
            deleteInput.id = myNum;
            myNum += myNum;
            deleteInput.onclick = removeDiv;
            var deleteTd = document.createElement("td");
            deleteTd.align = "right";
            document.getElementById(possibleId).appendChild(deleteTd);
            deleteTd.appendChild(deleteInput);

        }
    }

    document.getElementById("BarClick").onclick = function(){
        addItem("Bar");
    };

    document.getElementById("FooClick").onclick = function(){
        addItem("Foo");
    };

};






window.onload = function(){
    prepareEventHandlers();
};

编辑:

以下是jsfiddle的链接:http://jsfiddle.net/DTCav/xJVJR/1/

1 个答案:

答案 0 :(得分:3)

多么可怕的代码,但让我们开始吧:

  1. 验证您的HTML。即,您的<html>包含HTML5 <header>,而不是<head>

  2. 为什么要通过彼此使用本机JS和jQuery?为什么不坚持使用jQuery ??

  3. 无论如何,我的挑剔<aside>,以解决您的问题,将您的<table>代码更改为:

    <table>
        <tbody id="addedItems">
        </tbody>
    </table>
    
  4. 这是因为IE9会在空<tbody>中创建一个空的<table>,并将其用作表占位符,直接将<tr><td>标记注入{ {1}}