繁体   English   中英

如何获取/设置动态创建的文本框的值

[英]how to get/set value of dynamically created text box

我正在使用一种非常简单的方式在评论部分中表现得像fb的节日贺卡,我似乎无法获取并设置用户添加新内容后出现的动态创建文本框的值评论...我正在创建一个新的文本字段,并为ID附加一个数字以标识它,并且我可以在创建它的函数中设置值,但是一旦从另一个函数中查找它,代码就会中断。 有任何想法吗? 我想也许这将取决于该函数在文档中的位置,但对此不确定。 这里是一个链接:

精灵书

简而言之:

comment()包含以下修改输入字段的代码

// 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方法。

我已在下面使用了一些代码。 请参阅具有两个参数的输入调用ghostonBlur属性。 下面是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变量来保存原始数字值。 然后将ogfieldognumber传递给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

我输入的评论即将通过。

暂无
暂无

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

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