문제

I'm creating a checkbox dynamically and when the onclick function is executed I'm getting 'the function is undefined' error in the console. In stepping through, the code is read during the page load event but on click the error is thrown. I've tried loading the function into $(document).ready(); but get the same error:

*these functions are inside a function that's called on page load. 'chkBox' is the undefined function

function lyrBuild(lyrVar, lyrName) {
    $('<li>').attr({
        id: lyrVar.liID,
        class: 'layer'
    }).html($('<input>').attr({
        id: lyrVar.cbID,
        type: 'checkbox',
        onClick: "chkBox(" + "'" + lyrVar.liID + "'" + ")",
        checked: 'unchecked',
        name: lyrVar.Name,
        value: lyrVar.val
    })).appendTo('#layersList');
}

function chkBox(lyrId) {
    //switch statement goes here
} 
도움이 되었습니까?

해결책

You had no question but I give you an answer anyway ;)

"these functions are inside a function", which means that chkBox is not available when you call it that way. Use .click(), doc here, or .on(), or place chkBox outside the function, if possible.

다른 팁

Try declaring the dhkBox function like:

var chkBox = function(lyrId) {
    //switch statement goes here
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top