我想为我的html <textarea>设置一个默认值。我从材料中读到,要添加默认值,你必须做一些事情,如<textarea>这是默认文本</textarea>。我试过了,但没用。正确的做法是什么?
当前回答
下面是我的jsFiddle示例。这很好:
<textarea name='awesome'>Default value</textarea>
其他回答
以防万一,如果你在你的项目中使用Angular.js(就像我一样),并为你的<textarea>设置了ng-model,在里面设置默认值如下:
<textarea ng-model='foo'>Some default value</textarea>
...不会起作用!
您需要在相应的控制器中将缺省值设置为文本区域的ng-model或使用ng-init。
例1(使用ng-init):
var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', [ '$scope', function($scope){ // your controller implementation here }]); <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> <meta charset="utf-8"> <title>JS Bin</title> </head> <body ng-app='myApp'> <div ng-controller="MyCtrl"> <textarea ng-init='foo="Some default value"' ng-model='foo'></textarea> </div> </body> </html>
例2(不使用ng-init):
var myApp = angsnake .模块(“myApp”); myApp。控制器(“MyCtrl”,[“$scope”,function($scope) 美元的范围。foo =一些默认值; }); < !DOCTYPE html > < > html < head > <剧本剧本src = " https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js " > < / > <元charset = utf-8”> <片名本< - > JS头衔> < / head > <车身ng-app =“myApp > < div ng-controller = MyCtrl”> < textarea ng-model =“foo”> < / textarea > < / div > < / body > < / html >
占位符不能为文本区域设置默认值。你可以使用
<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检查空字段所需的工作。
几点注意事项和澄清:
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.
下面是我的jsFiddle示例。这很好:
<textarea name='awesome'>Default value</textarea>
你可以使用this.innerHTML。
<textarea name="message" rows =" 10" cols =" 100" onfocus="this。innerHTML= " ">在这里输入您的消息…textarea > < /
当文本区域被聚焦时,它基本上会使文本区域的innerHTML成为空字符串。
推荐文章
- 在输入type="number"时禁用webkit的旋转按钮?
- 如何在另一个元素之后添加一个元素?
- 我如何有效地解析HTML与Java?
- “ ”和“”有什么区别?
- 如何使用JavaScript代码获得浏览器宽度?
- 防止滚动条增加到Chrome页面的宽度
- 使用图像而不是单选按钮
- 我可以嵌套一个<按钮>元素内< >使用HTML5?
- 设置TextView文本从html格式的字符串资源在XML
- 为什么我的球(物体)没有缩小/消失?
- Twitter Bootstrap 3 Sticky Footer
- CSS在特定的“内联块”项之前/之后换行
- 很好的初学者教程socket.io?
- 点击下载Href图片链接
- HtmlSpecialChars在JavaScript中等价于什么?