首页 > 解决方案 > 如何修复未捕获的类型错误:无法读取 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>

标签: javascripthtmltypeerror

解决方案


value应该是小写。这就是为什么当您获取 nameField 节点值的文档时会出现未定义的原因。对于名称字段。

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

推荐阅读