简体   繁体   English

创建一个动态领域并获得价值

[英]creating a dynamic field and getting the value

i posted another question about this, but i think this one will be more concise. 我对此发表了另一个问题,但我认为这将更加简洁。

i've dynamically created a text field with javascript. 我已经用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. 我已经通过获取值来检查它是否在DOM中,但是当尝试从另一个函数访问它时,我得到了输入对象的null值。 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. 不会发生,因为在DOM中找不到[HTMLInputObject]。 Please help. 请帮忙。 Thanks! 谢谢!

is it actually possible to get the current value from a dynamically created text field? 实际上有可能从动态创建的文本字段中获取当前值吗?

Yes. 是。 It is just like any other element. 就像其他任何元素一样。 If it is in the DOM, it is accessed the same way. 如果它在DOM中,则以相同的方式访问它。

Verify the value of the comment_field variable. 验证comment_field变量的值。 You're making reference to different functions, so perhaps that variable is out of scope. 您正在引用不同的功能,因此该变量可能不在范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM