键入Error:getElementById..null with h:selectBooleanCheckbox

时间:2017-01-20 07:30:28

标签: javascript jsp

代码正在运行并使用

设置值
setCookie('newWindow', document.getElementById("myForm:header:"+1+":chekDeleted").checked? 1:0,100);

但是当我在循环中调用getElementById时,它表示类型错误

var count = 0;

for(i=1;i<totalRecords;i++)
{
    var chkBox = getElementById("myForm:header:"+i+":chekDeleted");
    if(chkBox.checked == true)
    {
        count++;
        if(count > 1)
        {
            setCookie('newWindow', document.getElementById("myForm:header:"+i+":chekDeleted").checked? 1:0,100);

        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试添加document.

for(i=1;i<totalRecords;i++)
{
        var chkBox = document.getElementById("myForm:header:"+i+":chekDeleted");
    if(chkBox.checked == true)
    {
        count++;
        if(count > 1)
        {
            setCookie('newWindow', document.getElementById("myForm:header:"+i+":chekDeleted").checked? 1:0,100);

        }
    }
}

如果仍然有错误,那是因为没有加载带有这些ID的元素。在这种情况下,chkBox会返回nullif(chkBox.checked == true)会返回错误未捕获的TypeError:无法读取属性'已检查'为空