如何获取/设置动态创建的文本框的值
我正在制作一张节日贺卡,其行为类似于 fb,以一种非常简单的方式,在评论部分,我似乎无法获取和设置用户添加新文本框后出现的动态创建的文本框的值评论...我正在创建一个新的文本字段,并附加一个用于标识它的 id 数字,我可以在创建它的函数中设置该值,但是一旦从另一个函数中查找它,代码就会中断。有什么想法吗?我认为这可能取决于该函数在文档中出现的位置,但不确定。这是一个链接:
简而言之:
comment() 包含以下修改输入字段的代码
<代码> // var subject = 'HI593F1' 或类似的东西;
// var current_comment = new Array() 并保持当前新评论框的计数
// 结果值如下所示:'comment-HI593F1-2'
var comment_field = '评论-'+主题+'-'+current_comment[主题];
document.getElementById(comment_field).value = '写评论...';
document.getElementById(comment_field).onblur = function() { Ghost('comment', subject); }
document.getElementById(comment_field).onfocus = function() { unghost('comment', subject); }
document.getElementById(comment_field).onkeypress = function() { text_color('comment', subject); }
unghost() 的工作原理如下:
function unghost(field, number) { // field = 'comment' ... this is 'comment' because this function modifies more than one field var ogfield = field; // if another comment is expanded if (current) { collapse_comment(current); } current = number; // like var comment field in the comment() function if (number) { field = field+"-"+number+"-"+current_comment[number]; } // below is where the code breaks ... values[ogfield] = 'Write a comment...'; // should look like this: document.getElementById('comment-HI593F1-2').value == 'Write a comment...' if (document.getElementById(field).value == values[ogfield]) { document.getElementById(field).value = \'\'; } // change the color of the field text text_color(field, number); }
i'm working on a holiday greeting card that behaves like fb in a very simple way and in the commenting portion, i can't seem to get and set the value of the dynamically created text box that appears after the user has added a new comment... i'm creating a new text field with an appended number for the id to identify it and i can set the value in the function that creates it, but once looking for it from another function, the code breaks. any ideas? i would figure that maybe this would be contingent on where the function occurs in the document but not sure about that. here's a link:
here it is in a nutshell:
comment() contains the following code that modifies the input field
// var subject = 'HI593F1' or something like that;
// var current_comment = new Array() and keeps count of the current new comment box
// this resulting value looks like this: 'comment-HI593F1-2'
var comment_field = 'comment-'+subject+'-'+current_comment[subject];
document.getElementById(comment_field).value = 'Write a comment...';
document.getElementById(comment_field).onblur = function() { ghost('comment', subject); }
document.getElementById(comment_field).onfocus = function() { unghost('comment', subject); }
document.getElementById(comment_field).onkeypress = function() { text_color('comment', subject); }
unghost() works like this:
function unghost(field, number) { // field = 'comment' ... this is 'comment' because this function modifies more than one field var ogfield = field; // if another comment is expanded if (current) { collapse_comment(current); } current = number; // like var comment field in the comment() function if (number) { field = field+"-"+number+"-"+current_comment[number]; } // below is where the code breaks ... values[ogfield] = 'Write a comment...'; // should look like this: document.getElementById('comment-HI593F1-2').value == 'Write a comment...' if (document.getElementById(field).value == values[ogfield]) { document.getElementById(field).value = \'\'; } // change the color of the field text text_color(field, number); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将预期值传递给
text_color
方法。我在下面获取了您的一些代码。请参阅输入的
onBlur
属性使用两个参数调用ghost
。下面是ghost
的主体,其中修改了field
参数,然后将其传递给text_color
- 后者又修改了值。我建议创建一个新的 ognumber 变量来保存原始数字值。然后将
ogfield
和ognumber
传递给text_color
。unghost
也遇到同样的问题。编辑
我使用的是 Chrome,这是我点击评论时发送的请求标头。
我输入的评论正在通过。
You're not passing in the expected value to the
text_color
method.I've taken some of your code below. See the
onBlur
attribute of the input callsghost
with the two parameters. Below is the body ofghost
, in it thefield
parameter is modified and then passed intotext_color
- which in turn modifies the value.I would suggest creating a new
ognumber
variable to hold the original number value. Then passogfield
andognumber
totext_color
.unghost
suffers the same problem.EDIT
I'm using Chrome, and here are the request headers sent when I click comment.
The comment I entered is coming through.