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
1 Answer
Reset to default 5Try
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
版权声明:本文标题:javascript - Cannot Read Property 'Elements' of Undefined - in JS Console - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741536302a2384044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论