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

发布于 2024-10-07 10:08:49 字数 1782 浏览 1 评论 0原文

我正在制作一张节日贺卡,其行为类似于 fb,以一种非常简单的方式,在评论部分,我似乎无法获取和设置用户添加新文本框后出现的动态创建的文本框的值评论...我正在创建一个新的文本字段,并附加一个用于标识它的 id 数字,我可以在创建它的函数中设置该值,但是一旦从另一个函数中查找它,代码就会中断。有什么想法吗?我认为这可能取决于该函数在文档中出现的位置,但不确定。这是一个链接:

Elfbook

简而言之:

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:

Elfbook

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-10-14 10:08:49

您没有将预期值传递给 text_color 方法。

我在下面获取了您的一些代码。请参阅输入的 onBlur 属性使用两个参数调用 ghost。下面是 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

我输入的评论正在通过。

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 calls ghost with the two parameters. Below is the body of ghost, in it the field parameter is modified and then passed into text_color - which in turn modifies the value.

<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); 
}

I would suggest creating a new ognumber variable to hold the original number value. Then pass ogfield and ognumber to text_color.

unghost suffers the same problem.

EDIT
I'm using Chrome, and here are the request headers sent when I click comment.

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

The comment I entered is coming through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文