Cannot Read Property 'Elements' of Undefin

2019-09-05 05:09发布

HTML form (outputed thru PHP)

$retVal .= "<form id= 'importSampleDataForm' name = 'importSampleDataForm' onsubmit = \"return false\" >\n";

    // Form content edited out; it contains several dozen checkboxes

$retVal .= sprintf("<input type='button' class='wn_button' onclick='SaveMotorSkills();' value='Import Selected'>\n");
$retVal .= "</form>\n";

JavaScript / Ajax

function SaveMotorSkills() // {{{
{
    // {{{ Ajax Header
    var httpRequest = CreateHttpRequest();
    if (!httpRequest) { 
        DialogFail('AJAX initialization error. ', 1 );
        return false; 
    } // }}}

    var params = '_SaveMotorSkills=1';
    for(i=0; i<document.importSampleDataForm.elements.length; i++) // line with error
    {
        params += "&" + document.importSampleDataForm.elements[i].name + 
            "=" + document.importSampleDataForm.elements[i].value ;
    }

    // edit out AJAX setup

    httpRequest.send(params);
    DialogSave("Saving...");
} // }}}

See "Line With Error Above" in for loop. JS Console says

Uncaught TypeError: Cannot read property 'elements' of undefined

I have looked at this code so much my eyes can't take it anymore. Does anyone else see anything I don't? I can add more code if needed.

1条回答
不美不萌又怎样
2楼-- · 2019-09-05 05:48

Try

document.getElementById('importSampleDataForm').elements.length

Also, nested forms are not allowed, so that also needs to be resolved.

查看更多
登录 后发表回答