简体   繁体   中英

"Uncaught TypeError: Cannot read property 'toString' of null"

When I debug this code, I get this error on the "else" part of the if-statement. I am not very familiar with java script so any help would be great :)

function doDDItemSelected() {
    var oSrc = event.srcElement
    var oCrit
    var oCritB
    var lstListItems
    var iCritPK = oSrc.getAttribute('CRIT_PK')
    oSrc.style.visibility = "hidden";
    oSrc.style.display = "none";

    if (oSrc.id.indexOf("lstCritB") != -1) {
        oCrit = $get('CritB' + iCritPK.toString());
        oCrit.value = oSrc.options[oSrc.selectedIndex].text;
    } else {
        oCrit = $get('Crit' + iCritPK.toString()); // error is in this line
        oCrit.value = oSrc.options[oSrc.selectedIndex].text;
        if (oCrit.onchange) {
            oCrit.onchange();
        }

        // Set the value of the end of range field if there is one
        try {
            oCritB = $get('CritB' + iCritPK.toString());
            if (oCritB.value == "") {
                if (oSrc.options[oSrc.selectedIndex].value != "(ALL)") {
                    oCritB.value = oSrc.options[oSrc.selectedIndex].text;
                }
            }
        } catch (e) {
            return;
        }
    }
}

Edit: I should add that this function is used in a selecting a drop down box in a web browser. The issue is that I cannot select items in the drop down box and have the selected item displayed in the same box. This code works in IE11 but not in Google Chrome.

Edit 2: Here is a screenshot of the debugger in google chrome在此处输入图片说明

嗨,尝试将事件作为参数传递

function doDDItemSelected(event) {

its because that variable is the result of a call to the method getAttribute which, will return null if it is not set. null does not contain the method 'toString', so the error is correctly being raised.

Perhaps thinking of doing an else if case which checks that the value is explicitly not null.

... 
}
else if ( iCritPK !== null ) { 
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM