JavaScript类型错误'childnodes'未定义 [英] JavaScript Type Error 'childnodes' undefined

查看:140
本文介绍了JavaScript类型错误'childnodes'未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要使用JavaScript在类型错误的情况下动态添加/删除HTML表中的行.

Need to Dynamically Add/Remove rows in HTML table using JavaScript getting a type error.

类型错误:无法读取未定义的属性"childNodes"?

Type Error: cannot read property 'childNodes' of undefined?

在deleteRow函数执行期间的运行时捕获此异常.

Catches this exception in run time during deleteRow function execution.

按添加行时,将创建一个动态行并将其添加到表中.然后选中该复选框并按Delete Row(删除行),该行将被删除.

On pressing Add Row, a dynamic row will be created and added in the table. And on selecting the checkbox and pressing Delete Row, the row will be removed.

以下是来源.

<html>
<head>
<script>
var b = new Array();
var entryListCounter = 0;
var counter = 0;

function insertEntry(f) {

var test = 0;

    //local array collects input values
    var a = new Array();

    a[0] = f.ticker.value;
    a[1] = f.cusip.value;
    a[2] = f.quantity.value;

    //store local array in entry list array
    b[entryListCounter] = a;

    entryListCounter++;


    if (a[0] == "" && a[1] == "" || a[2] == "") {
        console.log("val not filled");
    } 
    else if(a[0].length > 0 && a[1].length > 0){
        console.log("only fill one");
    }
    else {
        var table = document.getElementById("manualEntryInputTable");
        var row = table.insertRow(table.rows.length);

        var cell1 = row.insertCell(0);
        var t1 = document.createElement("input");
        t1.id = "t" + counter;
        cell1.appendChild(t1);

        var cell2 = row.insertCell(1);
        var t2 = document.createElement("input");
        t2.id = "c" + counter;
        cell2.appendChild(t2);

        var cell3 = row.insertCell(2);
        var t3 = document.createElement("input");
        t3.id = "q" + counter;
        t3.style = "text-align:right";
        cell3.appendChild(t3);

        var cell4 = row.insertCell(3);
        var t4 = document.createElement("input");
        t4.type = "checkbox";
        t4.name = "chkbox";
        cell4.appendChild(t4);

        f.quantity.value = "";
        f.cusip.value = "";
        f.ticker.value = "";
    }
}

        function deleteRow(e) {
            try {
            var table = document.getElementById("manualEntryInputTable");
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[3].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }




</script>

</head>

<body>
<form>
        <table id="manualEntryInputTable">
            <tr>
                <td><b>T</b></td>
                <td><b>C</b></td>
                <td><b>Q</b></td>
            </tr>
            <tr>
                <td class="label"><input size="" type="text" name="ticker"
                    value="" ></td>
                <td class="label"><input size="" type="text" name="cusip"
                    value=""></td>
                <td class="label"><input size="" type="text" name="quantity"
                     style="text-align: right;" value="">
                </td>
                <td><input type="button" onClick="insertEntry(this.form)" value="Add"/>
                </td>
                <td><input type="button" onClick="deleteRow(this.form)" value="Delete"/>
                </td>
            </tr>
        </table>
</form> 

</body>
</html>

推荐答案

通过查看null的条件检查,我可以这样重写它:

By looking at the conditional checking for null, I would rewrite it like this:

var chkbox = row.cells[3].childNodes.length ? row.cells[3].childNodes[0] : null;

这应该避免引发错误,但是如果索引3的单元格不存在,则可能无法删除该行.考虑检查右单元格索引row.cells[YOUR_CELL_INDEX_HERE].childNodes[0]

That should avoid the error being thrown, but might not get the row deleted if the cell with index 3 doesn't exist. Consider checking the value of the right cell index row.cells[YOUR_CELL_INDEX_HERE].childNodes[0]

这篇关于JavaScript类型错误'childnodes'未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆