如何在HTML表单中“预填充”textarea的值? [重复]

时间:2022-11-12 12:43:11

This question already has an answer here:

这个问题在这里已有答案:

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解决方案,但如果有必要,我会使用它。

4 个解决方案

#1


108  

<textarea>This is where you put the text.</textarea>

#2


25  

If your question is, how to fill a textarea:

如果你的问题是,如何填写textarea:

<textarea>
Here is the data you want to show in your textarea
</textarea>

#3


10  

It's a HTML5 tag and will work only with modern browsers :)

这是一个HTML5标签,仅适用于现代浏览器:)

<textarea placeholder="Add a comment..."></textarea>

#4


1  

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!

希望能给你一个答案和一个想法!

#1


108  

<textarea>This is where you put the text.</textarea>

#2


25  

If your question is, how to fill a textarea:

如果你的问题是,如何填写textarea:

<textarea>
Here is the data you want to show in your textarea
</textarea>

#3


10  

It's a HTML5 tag and will work only with modern browsers :)

这是一个HTML5标签,仅适用于现代浏览器:)

<textarea placeholder="Add a comment..."></textarea>

#4


1  

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!

希望能给你一个答案和一个想法!