【问题标题】:how to fix the Uncaught TypeError: Cannot read property 'Value' of null如何修复未捕获的类型错误:无法读取 null 的属性“值”
【发布时间】:2021-09-30 09:59:16
【问题描述】:

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

【问题讨论】:

  • 在调用getElementById()之前,你确保DOM已经完全加载了吗?
  • 修复这一行 -> let nameField = document.getElementById("nameField").value;将值更改为小写。

标签: javascript html typeerror


【解决方案1】:

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

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

【讨论】:

  • OMG 现在工作了,我犯的愚蠢错误感谢 RITIK SINGH
  • 很高兴它有帮助。
  • 我在过去五个小时内试图解决这个问题
【解决方案2】:
<input type="text" id="nameField"  class="form-control"
        placeholder="Enter here here">

您缺少引号。

【讨论】:

  • 很好看! .
  • 请不要回答错字问题。发表评论并将问题标记为错字。
  • 抱歉不知道,我是新人!以后会做的
  • 加引号后显示未定义
【解决方案3】:

我想你想要这个

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

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

【讨论】:

  • 我试过了,但在输出部分显示未定义
  • 这是因为 Saren 输入的 ID 为 nameField1 而不是 nameField
猜你喜欢
  • 2022-06-13
  • 2017-09-09
  • 1970-01-01
  • 2021-12-01
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多