如何获取/设置动态创建的文本框的值
我正在做一个非常简单的方式和评论部分中的行为像fb的节日贺卡,我似乎无法获取和设置值在用户添加新评论之后出现的动态创建的文本框......我创建了一个新的文本字段,其中包含用于识别它的id的附加数字,并且我可以在创建它的函数中设置该值,但一旦从另一个函数寻找它,代码就会中断。有任何想法吗?我会认为这可能取决于函数在文档中的位置,但不确定。这里有一个链接:如何获取/设置动态创建的文本框的值
这是概括地说:
评论()包含以下代码,修改输入字段
// 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()的工作原理是这个:
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); }
您没有将期望值传递给text_color
方法。
我已经采取了下面的一些你的代码。使用这两个参数查看输入调用ghost
的onBlur
属性。下面是ghost
的主体,其中field
参数被修改,然后传递到text_color
- 这反过来修改值。
<input type="text" id="comment-MS584C7-1" value="Write a comment..." style="width: 386px; height: 20px; border: 1px solid #c1dcc0; color: #666464; padding: 3px;" onBlur="ghost('comment', 'MS584C7');" onFocus="unghost('comment', 'MS584C7');" onkeypress="text_color('comment', 'MS584C7');" />
function ghost(field, number)
{
var ogfield = field;
if (number)
{
field = field+"-"+number+"-"+current_comment[number];
}
if (!document.getElementById(field).value)
{
document.getElementById(field).value = values[ogfield];
}
text_color(field, number);
}
我建议建立一个新的ognumber
变量来保存原来的数值。然后通过ogfield
和ognumber
至text_color
。
unghost
也存在同样的问题。
编辑 我使用的是Chrome浏览器,以下是点击评论时发送的请求标头。
Request URL:http://getpearson.com/nosesobright_comment.php
Request Method:POST
Status Code:200 OK
Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:90
Content-type:application/x-www-form-urlencoded
Cookie:PHPSESSID=------------------
Host:getpearson.com
Origin:http://getpearson.com
Referer:http://getpearson.com/nosesobright
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10
Form Data
subject:MS584C7
user:XP192R5
name:
avatar:undefined
attachment:undefined
comment:asdasdasd
Response Headers
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:155
Content-Type:text/html
Date:Mon, 13 Dec 2010 23:42:31 GMT
Keep-Alive:timeout=10, max=30
Server:Apache
Vary:Accept-Encoding
X-Powered-By:PHP/5.2.14
我输入的评论即将通过。
发布相关代码,我们将会看到。 – 2010-12-13 22:01:21
这是在提供的网址...只是查看源 – Lucas 2010-12-13 22:03:43
不,我不会那样做。请在此发布相关代码,否则您无法真正期望来自这里的人们提供多少帮助。 – 2010-12-13 22:09:27