[英]javascript variable undefined or null
couldn't find a specfic answer elsewhere. 在其他地方找不到合适的答案。 I'm totally new to JS and trying to pull a value out of a form and write it to the page.
我是JS的新手,试图从表单中提取一个值并将其写入页面。 The result when I try to write ProductName is undefined, and when I try to write ProductNameElement is null.
尝试写入ProductName时的结果是未定义的,而尝试写入ProductNameElement时的结果为null。 I'm sure it's to do with the form values being empty when the page loads but not sure after that...
我确定这与页面加载时表单值为空有关,但此后并不确定...
<script>
var ProductNameElement = document.getElementById("ProductName");
var ProductName = ProductNameElement.value;
function showname(){
document.write(ProductName);
}
</script>
<h2>Revenues</h2>
<div class="number">Product Name: <input type="text" id="ProductName" value=""></input></div>
<input type="button" value"showname" onclick="showname();"></input>
You are running the first two lines of your script too early BEFORE the elements in your page have been parsed and placed into the DOM. 在页面中的元素被解析并放入DOM之前,您正在运行脚本的前两行。 Because of that, the
ProductName
element doesn't exist yet when you're trying to find it with document.getElementById("ProductName");
因此,当您尝试使用
document.getElementById("ProductName");
查找ProductName
元素时,它尚不存在document.getElementById("ProductName");
. 。
Place your script right before the </body>
tag and then all your page elements will be available when you run your script. 将您的脚本放在
</body>
标记之前,然后在运行脚本时所有页面元素都将可用。 Or, just put all your code in the showname
function that isn't called until the click event. 或者,只需将您的所有代码放入
showname
函数中,直到单击事件才被调用。
function showname(){
var ProductNameElement = document.getElementById("ProductName");
var ProductName = ProductNameElement.value;
document.write(ProductName);
}
And, as others have said, using document.write()
after the documented has been loaded will cause the existing document to be cleared and a new empty document will be created. 而且,正如其他人所说,在加载文档后使用
document.write()
将导致现有文档被清除并创建一个新的空文档。 This is pretty much never what you want. 这几乎不是您想要的。 If you're just doing this for debugging, use
console.log(ProductName)
and look at the debug console. 如果您只是在进行调试,请使用
console.log(ProductName)
并查看调试控制台。
You'll have to get the element and the value inside the function, otherwise they aren't available, and the change of the value isn't caught 您必须在函数内获取元素和值,否则它们将不可用,并且不会捕获值的更改
<script>
function showname(){
var ProductNameElement = document.getElementById("ProductName");
var ProductName = ProductNameElement.value;
document.write(ProductName);
}
</script>
<h2>Revenues</h2>
<div class="number">
Product Name: <input type="text" id="ProductName" value="" />
</div>
<input type="button" value"showname" onclick="showname();" />
And inputs are self closing, and you should stop using document.write
, it will overwrite the entire document and remove everything that is currently there ! 输入是自动关闭的,您应该停止使用
document.write
,它将覆盖整个文档并删除当前存在的所有内容!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.