Javascript 使用 jQuery 更改文本区域值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10132205/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:00:15  来源:igfitidea点击:

Change Text Area Value with jQuery

javascriptjqueryhtml

提问by Hyman Davis

How could I change the value of a text area through jQuery. Here is a sample:

如何通过 jQuery 更改文本区域的值。这是一个示例:

var content = "Hello World";
//Code to set the value of the 
text area to content here.

How would I do this through jQuery or just javascript? An answer is appreciated, thanks. I tried this:

我将如何通过 jQuery 或仅使用 javascript 来做到这一点?感谢回答,谢谢。我试过这个:

var txtArea = document.getElementById('aTextArea');
txtArea.value = rolls;

However, that was just a shot in the dark.

然而,这只是黑暗中的一击。

回答by DG3

Assuming your textarea as

假设您的 textarea 为

<textarea id="textarea" rows="4" cols="50">
   sample textarea
</textarea>??????

jquery code to update textarea value would be

更新 textarea 值的 jquery 代码将是

 ?$(document).ready(function(){
    var content = "Hello World";
    $("#textarea").val(content);
});?

jsfiddle: http://jsfiddle.net/bhatlx/PuWH4/1/

jsfiddle:http: //jsfiddle.net/bhatlx/PuWH4/1/

回答by Christofer Eliasson

jQuery

jQuery

With jQuery you can use .val("Your new value")on textareas as well, even if the HTML-element doesn't have a value-attribute.

使用 jQuery,您也可以.val("Your new value")在 textareas 上使用,即使 HTML 元素没有 value-attribute。

$(function(){
   $("#id-of-textarea").val("test");
});?

Example: http://jsfiddle.net/sfxTt/

示例:http: //jsfiddle.net/sfxTt/

Plain JavaScript

纯 JavaScript

With plain JS, it could be done like this:

使用普通的 JS,可以这样做:

document.getElementById('id-of-textarea').value = "Hello";

Example: http://jsfiddle.net/sfxTt/1/

示例:http: //jsfiddle.net/sfxTt/1/

回答by I.Tyger

jQuery("textarea[name='message_text_reply']").val('Holla') 

This works very well for me where input attribute name is message_text_reply, the $("#message_text_reply")option is not working .

这对我来说非常有效,其中输入属性名称是message_text_reply,该$("#message_text_reply")选项不起作用。