创建动态领域并获取价值
我发布了关于此的另一个问题,但我认为这个会更简洁。
我用 javascript 动态创建了一个文本字段。我已经通过获取它的值来检查它是否在 DOM 中,但是当尝试从另一个函数访问它时,我得到的输入对象值为 null。实际上可以从动态创建的文本字段中获取当前值吗?
在一个函数内部:
comment_field = 'comment-HE382U3-2'; document.getElementById(comment_field).value = 'Write a comment...';
在另一个函数内部:
comment_field = 'comment-HE382U3-2'; if (document.getElementById(comment_field).value == 'Write a comment...') { document.getElementById(comment_field).value = '';}
不会发生,因为在 DOM 中找不到 [HTMLInputObject]。请帮忙。谢谢!
i posted another question about this, but i think this one will be more concise.
i've dynamically created a text field with javascript. i've checked to see that it's in the DOM by getting its value, but when trying to access it from another function i get a value of null for the input object. is it actually possible to get the current value from a dynamically created text field?
inside one function:
comment_field = 'comment-HE382U3-2'; document.getElementById(comment_field).value = 'Write a comment...';
inside the other:
comment_field = 'comment-HE382U3-2'; if (document.getElementById(comment_field).value == 'Write a comment...') { document.getElementById(comment_field).value = '';}
is not happening because there is no [HTMLInputObject] found in the DOM. Please help. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。它就像任何其他元素一样。如果它在 DOM 中,则以相同的方式访问它。
验证
comment_field
变量的值。您正在引用不同的函数,因此该变量可能超出范围。Yes. It is just like any other element. If it is in the DOM, it is accessed the same way.
Verify the value of the
comment_field
variable. You're making reference to different functions, so perhaps that variable is out of scope.