[英]document.getElementById returns null when element is part of a form
I have php
file that has some javascript inside. 我有php
文件里面有一些javascript。 Consider the following javascript code, which prints null
to the console (line 124 in the file): 考虑以下javascript代码,它向控制台输出null
(文件中的第124行):
console.log(document.getElementById('d_min'));
Below the script, I have the following form (line 140 in the file): 在脚本下面,我有以下表格(文件中的第140行):
<FORM name="DataCollect" ACTION="save_phase1.php" method="post">
<input type="hidden" name="d_min" id="d_min" value=""/>
</FORM>
Why is it that it could be null
? 为什么它可以为null
? Why can't it find that element? 为什么不能找到那个元素?
I FIXED THE PROBLEM: 我修复了问题:
I put the entire <form>
as a document.write
statement after the existing document.write
statements I had. 我把整个<form>
为document.write
的现有后声明document.write
的语句我有。
The DOM
is not fully loaded by the time the script executes. 脚本执行时, DOM
未完全加载。 Move your script below that form and see if you get better results. 将您的脚本移动到该表单下方,看看您是否获得了更好的结果。
在window.onload
函数中包装JavaScript代码。
I have no idea why it's not grabbing the field, but try giving the form an id foo
and using 我不知道为什么它没有抓住这个领域,但尝试给形式一个id foo
并使用
document.getElementById('foo').d_min
instead. 代替。 At least it will narrow the source of the problem. 至少它会缩小问题的根源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.