我想为我的html <textarea>设置一个默认值。我从材料中读到,要添加默认值,你必须做一些事情,如<textarea>这是默认文本</textarea>。我试过了,但没用。正确的做法是什么?
当前回答
你可以使用占位符属性,它不添加默认值,但可能是你正在寻找的:
<textarea placeholder="this text will show in the textarea"></textarea>
点击这里查看- http://jsfiddle.net/8DzCE/949/
重要提示(Jon Brave在评论中建议):
占位符属性不设置文本区域的值。相反,“占位符属性代表了一个简短的提示(一个单词或短语),旨在帮助用户在控件没有值时输入数据”[并且一旦用户单击进入文本区域,它就会消失]。它永远不会作为控件的“默认值”。如果你想这样做,你必须把想要的文本放在这里是实际的默认值,就像这里的其他答案一样
其他回答
几点注意事项和澄清:
placeholder='' inserts your text, but it is greyed out (in a tool-tip style format) and the moment the field is clicked, your text is replaced by an empty text field. value='' is not a <textarea> attribute, and only works for <input> tags, ie, <input type='text'>, etc. I don't know why the creators of HTML5 decided not to incorporate that, but that's the way it is for now. The best method for inserting text into <textarea> elements has been outlined correctly here as: <textarea> Desired text to be inserted into the field upon page load </textarea> When the user clicks the field, they can edit the text and it remains in the field (unlike placeholder=''). Note: If you insert text between the <textarea> and </textarea> tags, you cannot use placeholder='' as it will be overwritten by your inserted text.
你可以使用占位符属性,它不添加默认值,但可能是你正在寻找的:
<textarea placeholder="this text will show in the textarea"></textarea>
点击这里查看- http://jsfiddle.net/8DzCE/949/
重要提示(Jon Brave在评论中建议):
占位符属性不设置文本区域的值。相反,“占位符属性代表了一个简短的提示(一个单词或短语),旨在帮助用户在控件没有值时输入数据”[并且一旦用户单击进入文本区域,它就会消失]。它永远不会作为控件的“默认值”。如果你想这样做,你必须把想要的文本放在这里是实际的默认值,就像这里的其他答案一样
占位符不能为文本区域设置默认值。你可以使用
<textarea rows="10" cols="55" name="description"> /*Enter default value here to display content</textarea>
如果您将它用于数据库连接,则它是标记。如果使用php以外的其他语言,则可以使用不同的语法。对于php:
例如:
<textarea rows="10" cols="55" name="description" required><?php echo $description; ?></textarea>
Required命令最大限度地减少了使用php检查空字段所需的工作。
下面是我的jsFiddle示例。这很好:
<textarea name='awesome'>Default value</textarea>
你也可以添加"value"属性并设置如下:
<textarea value="your value"> </textarea>
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击