![](/img/trans.png)
[英]How to Fix “Uncaught TypeError: Cannot read property 'value' of null” error?
[英]how to fix the Uncaught TypeError: Cannot read property 'Value' of null
我正在尝试修复错误,但它仍然说 Uncaught TypeError: Cannot read property 'Value' of null in console here is html and js
function generateCV() { // console.log("hello"); let nameField = document.getElementById("nameField").Value; let nameT1 =document.getElementById("nameT1"); nameT1.innerHTML=nameField; //direct document.getElementById('nameT2').innerHTML=nameField }
<div> <div class="form-group"> <label for="nameField1" >Your Name</label> <input type="text" id="nameField" class="form-control" placeholder="Enter here here"> </div> <div class="container text-center mt-3"> <button onclick="generateCV()" class="btn btn-primary btn-lg">Generate cv</button> </div> </div> <div class="container"> <p id="nameT1">Robin</p> </div>
value
应为小写。 这就是为什么当你获得 nameField 节点值的文档时你得到 undefined 的原因。 用于名称字段。
let nameField = document.getElementById("nameField").value;
<input type="text" id="nameField" class="form-control"
placeholder="Enter here here">
你少了一个引号。
我想你想要这个
let nameField = document.getElementById("nameField")?.value;
并在搜索 document.getElementById 中添加好的 id 因为你的 html 没有你搜索的 id 所以它返回 null 并且你检查 null 的值所以它也会崩溃......使用小写的值;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.