JavaScript“Uncaught TypeError:无法使用动态创建的复选框读取属性'已检查'。”

时间:2015-08-14 15:00:41

标签: javascript checkbox

所以我之前看过Uncaught TypeError: Cannot read property 'checked' of null个问题。但这些都没有帮助我。所有这些答案似乎都与页面完全加载之前的代码加载有关,或者是否有引号或其他内容。

整个程序必须处于window.onload函数中才能使其主要部分工作(保持div隐藏等等),所以也许这与它有关?另外,我必须动态创建复选框的ID。

我遇到问题的代码看起来像这样:

var list = [document.getElementById("div1"), document.getElementById("div2")];
var form = [document.createElement("form"), document.createElement("form")];
var tradeValue = [0, 0];
var toBeTraded = {
    name: [[], [], [], [], []],
    x: [[], [], [], [], []],
    yru: [[], [], [], [], []],
    type: [[], [], [], [], []],
    value: [[], [], [], [], []],
    w: [[], [], [], [], []]
};
var player = {
    pNames: [[], [], [], [], []],
    pType: [[], [], [], [], []],
    pX: [[], [], [], [], []],
    pYRU: [[], [], [], [], []],
    pValue: [[], [], [], [], []],
};
var tradeInitiated = 0;
var trdsel = document.getElementById("tp2");
var tradee;
    var trade = {
    list: function (p, z) {
        for (var i = 0; i < player.pNames[p].length; i++) {
            var label = document.createElement("label");
            var checkBox = document.createElement("input");
            checkBox.type = "checkbox";
            label.innerHTML = (player.pNames[p][i] + "    ");
            checkBox.id = z + "_" + i;
            label.appendChild(checkBox);
            form[z].appendChild(label);
        }
        list[z].appendChild(form[z]);
    },
    calc: function (k, z) {
        for (var i = 0; i < player.pNames[k].length; i++) {
            if (document.getElementById(z + "_" + i).checked === true) {
                tradeValue[z] = tradeValue[z] + player.pValue[k][i];
            }
        }
    },
    exch: function (k, l, z) {
        for (var i = 0; i < player.pNames[k].length; i++) {
            if (document.getElementById(z + "_" + i).checked === true) {
                if (player.pType[k][i] === "property") {
                    prop.id[player.pX[k][i]][player.pYRU[k][i]] = l;
                } else if (player.pType[k][i] === "rr") {
                    rr.id[player.pYRU[k][i]] = l;
                } else if (player.pType[k][i] === "utility") {
                    utils.id[player.pYRU[k][i]] = l;
                }
                monopolyTest();
                toBeTraded.name[k].push(player.pNames[k][i]);
                toBeTraded.x[k].push(player.pX[k][i]);
                toBeTraded.yru[k].push(player.pYRU[k][i]);
                toBeTraded.type[k].push(player.pType[k][i]);
                toBeTraded.value[k].push(player.pValue[k][i]);
                toBeTraded.w[k].push(i);
            }
        }
        trade.exchAct(k, l, z);
    },
    exchAct: function (k, l, z) {
        for (var i = 0; i < toBeTraded.name[k].length; i++) {
            player.pNames[l].push(toBeTraded.name[k][i]);
            player.pX[l].push(toBeTraded.x[k][i]);
            player.pYRU[l].push(toBeTraded.yru[k][i]);
            player.pType[l].push(toBeTraded.type[k][i]);
            player.pValue[l].push(toBeTraded.value[k][i]);
            tradeValue[z] = tradeValue[z] + toBeTraded.value[k][i];
            player.pNames[k].splice(toBeTraded.w[i], 1);
            player.pX[k].splice(toBeTraded.w[i], 1);
            player.pYRU[k].splice(toBeTraded[i], 1);
            player.pType[k].splice(toBeTraded[i], 1);
            player.pValue[k].splice(toBeTraded[i], 1);
        }
        toBeTraded.name = [[], [], [], [], []];
        toBeTraded.x = [[], [], [], [], []];
        toBeTraded.yru = [[], [], [], [], []];
        toBeTraded.type = [[], [], [], [], []];
        toBeTraded.value = [[], [], [], [], []];
        toBeTraded.w = [[], [], [], [], []];
    },
    prep: function () {
        tradee = document.getElementById("tp2").value;
        if (tradeInitiated === 0) {
            var tradeConfirmation = confirm("Would you like to trade with Player " + tradee + "?");
            if (tradeConfirmation === true) {
                tradeInitiated = 1;
                menu("trade");
                trade.list(turn, 0);
                trade.list(tradee, 1);
                trdsel.innerHTML = "";
            }
        }
        return tradee;
        $("#commenceTrade").on("click", function () { trade.act(); });
    },
    act: function () {
        alert("Player " + turn + ", allow Player " + tradee + " to access the computer to confirm the trade.");
        trade.calc(turn, 0);
        trade.calc(tradee, 1);
        var tradeIsOkay = confirm("Player " + tradee + ", look carefully. Are you alright with this trade? (Trade Value: " + tradeValue[0] + " vs. " + tradeValue[1] + ".)");
        if (tradeIsOkay === true) {
            trade.exch(turn, tradee, 0);
            trade.exch(tradee, turn, 1);
            trade.retSetup();
            console.log(prop.mnplTF);
            tradeValue[0] = 0;
            tradeValue[1] = 0;
        } else {
            tradeValue[0] = 0;
            tradeValue[1] = 0;
        }
    }
};

奇怪的问题是它在trade.calc但不在trade.exch。有什么明显的东西让我失踪吗?我对JavaScript很陌生,但我的代码看起来对我来说。

此外,按下按钮会调用trade.act(),但只会在trade.exch()处中断。

0 个答案:

没有答案