admin管理员组

文章数量:1291718

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.

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.

Share Improve this question edited Oct 25, 2013 at 22:33 TheLettuceMaster asked Oct 25, 2013 at 22:18 TheLettuceMasterTheLettuceMaster 15.7k50 gold badges158 silver badges266 bronze badges 2
  • Are you running the function SaveMotorSkills from the footer, or after the page/html is on the screen? You should be. – Brian Commented Oct 25, 2013 at 22:29
  • It is run from a separate .js document and the form is already displayed on the screen. When you click the button on bottom of form, this is when it is ran. – TheLettuceMaster Commented Oct 25, 2013 at 22:32
Add a ment  | 

1 Answer 1

Reset to default 5

Try

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

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

本文标签: javascriptCannot Read Property 39Elements39 of Undefinedin JS ConsoleStack Overflow