【问题标题】:Microsoft JScript runtime error: Unable to get value of the property 'checked': object is null or undefined only in IE9Microsoft JScript 运行时错误:无法获取属性“已检查”的值:对象为空或仅在 IE9 中未定义
【发布时间】:2013-02-14 09:36:05
【问题描述】:

我必须检查复选框中的项目,然后单击添加按钮,该按钮会将选中的项目添加到一个框中。检查复选框中的项目并单击添加按钮后,出现以下错误。它仅在 IE9 中发生。

Microsoft JScript 运行时错误:无法获取“已检查”属性的值:对象为空或未定义

我的js页面代码是:

document.onkeyup = fnRemoveSelectedItems;
//Global variables 
var cssGridrow="gridrow";
var cssGridalteraterow="gridalteraterow";
var selectedItems=new String();
var pickItemSelected="1";
var isOnsiteLoc = 0;

/*Function that adds the selected items to the text box in the pick up box */
function fnPickUpAdd(btnOKClientId, lstFromClientId, txtBoxClientId, ColumnToTake) {
    //enable the ok button
    document.getElementById(btnOKClientId).disabled=false;
    //From grid Object
    var lstFrom=document.getElementById(lstFromClientId);
    //to textbox Object
    var txtBoxObject=document.getElementById(txtBoxClientId);

    //If the lstFromClienId is location then iteration occurs.
    if (lstFromClientId.search(/Location/i) > 0) {
        //Iterating only with in the grid rows to check if the location is onsite
        for (var i = 1; i < lstFrom.rows.length; i++) {
            var CheckBoxId = lstFrom.rows[i].cells[0].childNodes[0].id;
            if (document.getElementById(CheckBoxId).checked && lstFrom.rows[i].cells[6].innerText != null && lstFrom.rows[i].cells[6].innerText == "True") {
                isOnsiteLoc = 1;
                break;
            }
        }
    }
    //Iterating only with in the grid rows to get the selected SOIDs
    for(var i=0;i<lstFrom.rows.length; i++) {
        if(lstFrom.rows[i].className==cssGridrow || lstFrom.rows[i].className==cssGridalteraterow) {
            var childRowCheckBoxId=lstFrom.rows[i].cells[0].childNodes[0].id;
            if(childRowCheckBoxId!= "") {
                if(document.getElementById(childRowCheckBoxId).checked) {
                    //locationmatchFor = "1" for all scenarios except for location pick up in search
                    // so for this we would take the value in cells[1]
                    if(ColumnToTake=='1'){ //From Preferences 
                        if(findMatchValue(txtBoxObject, lstFrom.rows[i].cells[1].innerText)==false) {
                            //show that in the text box
                            txtBoxObject.innerHTML+="<span onclick='fnSelectItem(this)' isItem=1>"+lstFrom.rows[i].cells[1].innerText+";</span>";

                            //Form this to pass the selected items to the parent page
                            //selectedItems+=(lstFrom.rows[i].cells[1].innerText)+":"+(lstFrom.rows[i].cells[2].innerText);
                            selectedItems+=(lstFrom.rows[i].cells[1].innerText);
                            selectedItems+=";";
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 错误发生在代码墙的哪一行?你看什么来找出问题? (我的意思是,很明显,您在 getElementById 调用之一中查找的 id 无效。)当您逐步使用浏览器内置的调试器时,您会看到什么?
  • 你能用作用于这个脚本的 html 和事件创建一个小提琴吗?
  • console.log(lstFrom.rows[i].cells[0].childNodes[0].nodeType),然后再尝试获取idnodeType 应该是 1,而不是 3

标签: javascript internet-explorer-9 runtime-error


【解决方案1】:

可能是您在以下几行中尝试访问的 DOM 属性 id 没有:

var CheckBoxId = lstFrom.rows[i].cells[0].childNodes[0].id;

var childRowCheckBoxId=lstFrom.rows[i].cells[0].childNodes[0].id;

id 是一个属性吗?如果是这样,你可以尝试做

getAttribute('id')

【讨论】:

    【解决方案2】:

    在 html 的头部添加以下内容:

    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    

    【讨论】:

      【解决方案3】:

      我的猜测是,在您的 For 循环中,您正在遍历网格的最后一行。检查Length 属性,看看它是否在计数中包含“页码行”。 i &lt; Length - 1 为我工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多