【问题标题】:Cannot populate input field without having to inspect input field element first?无需先检查输入字段元素就无法填充输入字段?
【发布时间】:2020-06-29 02:15:24
【问题描述】:

我正在尝试使用

填充表单的“名字”字段

document.getElementById('u_4SM_4607').value = 'Peet';

在我的 Google Chrome 浏览器的控制台中。

我在运行命令填写“名字”字段时收到此错误:

VM183:1 Uncaught TypeError: Cannot set property 'value' of null
    at <anonymous>:1:45

但是,如果我检查“名字”字段,然后运行document.getElementById('u_4SM_4607').value = 'Peet';,它会起作用并且会用“Peet”填充“名字”字段。

为什么我会得到那个错误代码,为什么它只有在我首先检查字段时才有效?

这是“名字”字段的input 元素:

<input name="u_4SM_4607" id="u_4SM_4607" type="text" value="" class=" u-full-width" placeholder="Your first name" aria-required="true">

这是我尝试练习的 Web 表单的 URL:

https://formsmarts.com/html-form-example

【问题讨论】:

    标签: javascript html dom input getelementbyid


    【解决方案1】:

    我认为这是因为表单是使用 iframe 从外部源嵌入的。 它就像另一个网页。所以如果你不参考这个网页,它就不起作用。

    【讨论】:

      猜你喜欢
      • 2017-07-01
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 2017-06-03
      • 2021-11-10
      • 2013-06-18
      • 2021-10-17
      • 2019-12-27
      相关资源
      最近更新 更多