简体   繁体   English

javascript变量未定义或为null

[英]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();" />

FIDDLE 小提琴

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.

相关问题 JavaScript 中的“未定义”或“空”变量 - 'Undefined' or 'Null' Variable in JavaScript 潜在的null / undefined变量的访问属性-javascript - accessing property of potential null/undefined variable - javascript Javascript变量检查:(空或未定义)与布尔值 - Javascript variable checking: (null or undefined) vs boolean 设置 JavaScript 变量 = null,还是保持未定义? - Set JavaScript variable = null, or leave undefined? 如何检查 JavaScript 中的未定义或 null 变量? - How to check for an undefined or null variable in JavaScript? 在JavaScript中测试变量是否为null,而不会导致未定义的错误 - Testing variable for null in javascript without causing undefined error 从本地JSON文件加载后,javascript变量为“ null”或“ undefined” - javascript variable is 'null' or 'undefined' after loading it from a local JSON file JavaScript 在未定义和 null 时验证不同的变量类型 - JavaScript validates variable types different when they are undefined and null 没有初始值的JavaScript变量在一种情况下返回null,而在另一种情况下未定义 - JavaScript variable with no initial value returns null in one case and undefined in another 变量为null或未定义时的IE9 JavaScript错误 - IE9 javascript error when variable is null or undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM