Textarea默认值

时间:2012-04-12 17:54:02

标签: jquery html css

我一直在使用以下内容在文本字段中放置默认值

<textarea name="message" id="message" cols="30" rows="5" value="Comment"
    class="form_text text-input shadow" title="Comment"
    onblur="if (this.value == '') {this.value = 'Comment';}" 
    onfocus="if (this.value == 'Comment') {this.value = '';}">
</textarea>

它部分有效。该字段最初不显示默认值,但如果您在其中单击然后输出,而不键入任何内容,则会显示“注释”。

我知道如何修改它,以便在表单加载时显示“Comment”。

6 个答案:

答案 0 :(得分:4)

不要使用价值使用:

<textarea name="message" id="message" cols="30" rows="5"
    class="form_text text-input shadow" title="Comment"
    onblur="if (this.innerHTML == '') {this.innerHTML = 'Comment';}" 
    onfocus="if (this.innerHTML == 'Comment') {this.innerHTML = '';}">
Comment
</textarea>

答案 1 :(得分:3)

<textarea>default value</textarea>

这应该可以解决问题。

答案 2 :(得分:3)

Textarea默认文本必须包含在标签

<textarea> this is my default text </textarea>

答案 3 :(得分:2)

<textarea>元素没有value属性:默认值是您在open标记和close标记之间写的内容:

<textarea>My default value</textarea>

在Javascript中,您可以使用.innerHTML(或使用带.val()的jQuery)访问此文本。

对于您尝试实现的行为,您可以使用HTML5 placeholder attribute并使用jQuery polyfill for old browsers

答案 4 :(得分:1)

textarea有innerHTML而不是value属性。(value =“Comment”不是设置值的正确方法)

尝试,

<textarea name="message" id="message" cols="30" rows="5" 
   class="form_text text-input shadow" title="Comment"  
   onblur="if (this.value == '') {this.value = 'Comment';}" 
   onfocus="if (this.value == 'Comment') {this.value = '';}">
   Comment
</textarea>

答案 5 :(得分:1)

<textarea>Value</textarea>

或者,如果您通过jquery使用$(document).ready(),则可以在加载时通​​过javascript分配值。

相关问题