如何在 HTML 表单中“预填充”文本区域的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2231936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I "pre-fill" the value of a textarea in an HTML form?
提问by jasonmklug
I'm creating a simple back-end app through which the user can create/update/delete database rows (in this case, job listings).
我正在创建一个简单的后端应用程序,用户可以通过它创建/更新/删除数据库行(在本例中为职位列表)。
When it comes time for the user to edit an existing listing, I'm trying to pre-fill most of the HTML form with the data from that existing row. I've done this sucessfully for text inputs using the "value" property, and with selects using a bit of php in each option tag: if([conditionforoption]){echo'selected'}.
当需要用户编辑现有列表时,我会尝试使用该现有行中的数据预填充大部分 HTML 表单。我已经成功地使用“value”属性为文本输入完成了这项工作,并在每个选项标签中使用了一些 php:if([conditionforoption]){echo'selected'}。
The input type that I'm having trouble pre-filling is the textarea... any thoughts on how to get the existing data (a long varchar string) to be present in the textarea input when the user loads the page?
我在预填充时遇到问题的输入类型是 textarea ...关于如何在用户加载页面时在 textarea 输入中显示现有数据(长 varchar 字符串)的任何想法?
I'm trying to stay away from a javascript solution if at all possible, but I'll use it if necessary.
如果可能的话,我试图远离 javascript 解决方案,但我会在必要时使用它。
回答by AGoodDisplayName
<textarea>This is where you put the text.</textarea>
回答by Tim
If your question is, how to fill a textarea:
如果您的问题是,如何填充 textarea:
<textarea>
Here is the data you want to show in your textarea
</textarea>
回答by Valentin Borisov
It's a HTML5 tag and will work only with modern browsers :)
这是一个 HTML5 标签,仅适用于现代浏览器 :)
<textarea placeholder="Add a comment..."></textarea>
回答by KarteMushroomPandas
To fill out a textarea's value, you insert text inside the tag like this:
要填写 textarea 的值,请在标签内插入文本,如下所示:
<textarea>Example of content</textarea>
In the code, the "Example of content" text will become the textarea's value. If want to add to the value, that is, take a value and add another string or data type do it, you would do this in JavaScript:
在代码中,“内容示例”文本将成为 textarea 的值。如果要添加到值,即取一个值并添加另一个字符串或数据类型,您可以在 JavaScript 中执行此操作:
<textarea id="test">Example of</textarea>
<!--I want to say "content" in the textarea's value, so ultimately it will say
Example of content. Pressing the "Add String To Value" button again will add another "content" string onto the value.-->
<input type="button" value="Add String To Value" onclick="add()"/>
<!--The above will call the function that'll add a string to the textarea's value-->
<script>
function add() {
//We first get the current value of the textarea
var x = document.getElementById("test").value;
//Then we concatenate the string "content" onto it
document.getElementById("test").value = x+" content";
}
</script>
Hope that gave you both an answer and an idea!
希望给你一个答案和一个想法!