如何修复未捕获的类型错误:无法读取 null 的属性“值”

时间:2021-07-23 08:37:20

标签: javascript html typeerror

我正在尝试修复错误,但它仍然说 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>

3 个答案:

答案 0 :(得分:2)

value 应该是小写的。这就是为什么当你获得 nameField 节点值的文档时你得到 undefined 的原因。用于名称字段。

 let nameField = document.getElementById("nameField").value;

答案 1 :(得分:1)

<input type="text" id="nameField"  class="form-control"
        placeholder="Enter here here">

您遗漏了一个引号。

答案 2 :(得分:-1)

我想你想要这个

let nameField = document.getElementById("nameField")?.value;

并在搜索 document.getElementById 中添加好的 id,因为您的 html 没有您搜索的 id,因此它返回 null 并且您检查 null 的值因此它崩溃 也...使用小写的值;)