向文本框添加默认值

发布于 2024-11-19 16:20:27 字数 1257 浏览 0 评论 0原文

我使用以下脚本将默认值添加到输入文本框:

$('.default-value').each(function() {
    var default_value = this.value;
    $(this).css('color', '#746F66'); // this could be in the style sheet instead
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', 'black');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#746F66');
            this.value = default_value;
        }
    });
});

使用以下形式:

<form method="post" action="contactengine.php">
    <input type="textbox" class="default-value" name="Name" id="Name" value="Full Name">

    <input type="textbox" class="default-value" name="Email" id="Email" value="Email">

    <textarea name="Message" class="default-value" name="Message" id="Message" value="Your message"></textarea>

    <input type="submit" name="submit" id="submit" value="Submit" class="submit-button" />
</form>

默认值对于文本框工作正常,但我无法弄清楚如何将默认值添加到文本区域中。目前,文本区域中没有显示任何内容,如此处所示。

有人可以帮忙解决这个问题吗?

谢谢,

尼克

I am using the following script to add default values to input text boxes:

$('.default-value').each(function() {
    var default_value = this.value;
    $(this).css('color', '#746F66'); // this could be in the style sheet instead
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', 'black');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#746F66');
            this.value = default_value;
        }
    });
});

With the following form:

<form method="post" action="contactengine.php">
    <input type="textbox" class="default-value" name="Name" id="Name" value="Full Name">

    <input type="textbox" class="default-value" name="Email" id="Email" value="Email">

    <textarea name="Message" class="default-value" name="Message" id="Message" value="Your message"></textarea>

    <input type="submit" name="submit" id="submit" value="Submit" class="submit-button" />
</form>

The default values work fine for the textboxes, but I can't work out how to get the default value into the textarea. At the moment nothing is showing up in the textarea, as you can see here.

Could someone help with this?

Thanks,

Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

堇色安年 2024-11-26 16:20:27

尝试更改此行:

var default_value = this.value;

至:

var default_value = $(this).val();

这是因为 textarea 没有 value 属性,要为该元素分配默认文本,只需将文本放在开头之间,以及元素的结束标签,如下所示:

<textarea name="Message" class="default-value" name="Message" id="Message">Your message.</textarea>

JS Fiddle demo


Edited after testing, in Chromium 12/Ubuntu 11.04, to discover that as far as JavaScript is concerned (at least for this browser/platform) var default_value = this.value; works to assign the default_value correctly.

JS Fiddle 演示

Try changing this line:

var default_value = this.value;

To:

var default_value = $(this).val();

This is because a textarea doesn't have a value attribute, to assign a default text to this element simple place the text between the opening, and closing, tags of the element, as such:

<textarea name="Message" class="default-value" name="Message" id="Message">Your message.</textarea>

JS Fiddle demo.


Edited after testing, in Chromium 12/Ubuntu 11.04, to discover that as far as JavaScript is concerned (at least for this browser/platform) var default_value = this.value; works to assign the default_value correctly.

JS Fiddle demo.

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