在 jQuery 中设置 textarea 的值

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

Set value of textarea in jQuery

jquerytextarea

提问by GONeale

I am attempting to set a value in a textarea field using jquery with the following code:

我正在尝试使用带有以下代码的 jquery 在 textarea 字段中设置一个值:

$("textarea#ExampleMessage").attr("value", result.exampleMessage);

The issue is, once this code executes, it is not altering the text in the textarea?

问题是,一旦执行此代码,它不会更改 textarea 中的文本?

However when performing an alert($("textarea#ExampleMessage").attr("value"))the newly set value is returned?

但是当执行一个alert($("textarea#ExampleMessage").attr("value"))新设置的值时会返回吗?

回答by enobrev

Have you tried val?

你试过val吗?

$("textarea#ExampleMessage").val(result.exampleMessage);

回答by danivalentin

Textarea has no value attribute, its value comes between tags, i.e: <textarea>my text</textarea>, it is not like the input field (<input value="my text" />). That's why attr doesn't work :)

Textarea 没有 value 属性,它的值出现在标签之间,即: <textarea>my text</textarea>,它不像输入字段 ( <input value="my text" />)。这就是为什么 attr 不起作用 :)

回答by CallMeLaNN

$("textarea#ExampleMessage").val()in jquery just a magic.

$("textarea#ExampleMessage").val()在 jquery 中只是一个魔术。

You should notice that textareatag using inner htmlto display and not in value attributejust like input tag.

你应该注意到textarea标签使用内部 html来显示,而不是像 input 标签一样在 value 属性中

<textarea>blah blah</textarea>
<input type="text" value="blah blah"/>

You should use

你应该使用

$("textarea#ExampleMessage").html(result.exampleMessage)

or

或者

$("textarea#ExampleMessage").text(result.exampleMessage)

depend on if you want to display it as html tags or plain text.

取决于您想将其显示为 html 标签还是纯文本。

回答by Mik

Oohh come on boys! it works just with

哦,来吧男孩!它只适用于

$('#your_textarea_id').val('some_value');

回答by Jomit

I think this should work :

我认为这应该有效:

$("textarea#ExampleMessage").val(result.exampleMessage);

回答by Michel

i had the same question so i decided to try it in the current browsers (we're one and a half year later in time after this question), and this (.val) works

我有同样的问题,所以我决定在当前的浏览器中尝试它(我们在这个问题之后一年半),并且这个 ( .val) 有效

$("textarea#ExampleMessage").val(result.exampleMessage); 

for

为了

  • IE8
  • FF 3.6
  • FF4
  • Opera 11
  • Chrome 10
  • IE8
  • FF 3.6
  • FF4
  • 歌剧11
  • 铬 10

回答by Dan Ochiana

On android .valand .htmldidn't work.

在android上.val.html没有工作。

$('#id').text("some value")

did the job.

完成了工作。

回答by dave

I had same issue and this solution didn't work but what worked was to use html

我有同样的问题,这个解决方案不起作用,但有效的是使用 html

$('#your_textarea_id').html('some_value');

回答by Luka Ramishvili

textarea doesn't store values as

textarea 不将值存储为

<textarea value="someString">

instead, it stores values in this format:

相反,它以这种格式存储值:

<textarea>someString</textarea>

So attr("value","someString")gets you this result:

所以attr("value","someString")得到你这个结果:

<textarea value="someString">someOLDString</textarea>.

try $("#textareaid").val()or $("#textareaid").innerHTMLinstead.

尝试$("#textareaid").val()$("#textareaid").innerHTML代替。

回答by Mathieua

There the problem : I need to generate html code from the contain of a given div. Then, I have to put this raw html code in a textarea. When I use the function $(textarea).val() like this :

问题是:我需要从给定 div 的包含生成 html 代码。然后,我必须将此原始 html 代码放在 textarea 中。当我像这样使用 $(textarea).val() 函数时:

$(textarea).val("some html like < input type='text' value='' style="background: url('http://www.w.com/bg.gif') repeat-x center;" /> bla bla");

$(textarea).val("some html like < input type='text' value='' style="background: url('http://www.w.com/bg.gif') repeat-x center; " /> bla bla");

or

或者

$('#idTxtArGenHtml').val( $('idDivMain').html() );

$('#idTxtArGenHtml').val( $('idDivMain').html());

I had problem with some special character ( & ' " ) when they are between quot. But when I use the function : $(textarea).html() the text is ok.

当它们在 quot 之间时,我遇到了一些特殊字符 ( & ' " ) 的问题。但是当我使用函数时:$(textarea).html() 文本没问题。

There an example form :

有一个示例表单:

<FORM id="idFormContact" name="nFormContact" action="send.php" method="post"  >
    <FIELDSET id="idFieldContact" class="CMainFieldset">
        <LEGEND>Test your newsletter&raquo; </LEGEND> 
        <p>Send to &agrave; : <input id='idInpMailList' type='text' value='[email protected]' /></p>
        <FIELDSET  class="CChildFieldset">
            <LEGEND>Subject</LEGEND>
            <LABEL for="idNomClient" class="CInfoLabel">Enter the subject: *&nbsp</LABEL><BR/>
          <INPUT value="" name="nSubject" type="text" id="idSubject" class="CFormInput" alt="Enter the Subject" ><BR/>
    </FIELDSET>
    <FIELDSET  class="CChildFieldset">
        <INPUT id="idBtnGen" type="button" value="Generate" onclick="onGenHtml();"/>&nbsp;&nbsp;
          <INPUT id="idBtnSend" type="button" value="Send" onclick="onSend();"/><BR/><BR/>
            <LEGEND>Message</LEGEND>
                <LABEL for="idTxtArGenHtml" class="CInfoLabel">Html code : *&nbsp</LABEL><BR/>
                <span><TEXTAREA  name="nTxtArGenHtml" id="idTxtArGenHtml" width='100%' cols="69" rows="300" alt="enter your message" ></TEXTAREA></span>
        </FIELDSET>
    </FIELDSET>
</FORM>

And javascript/jquery code that don't work to fill the textarea is :

无法填充 textarea 的 javascript/jquery 代码是:

function onGenHtml(){
  $('#idTxtArGenHtml').html( $("#idDivMain").html()  );
}

Finaly the solution :

最后的解决方案:

function onGenHtml(){
  $('#idTxtArGenHtml').html( $("#idDivMain").html() );
  $('#idTxtArGenHtml').parent().replaceWith( '<span>'+$('#idTxtArGenHtml').parent().html()+'</span>');
}

The trick is wrap your textarea with a span tag to help with the replaceWith function. I'm not sure if it's very clean, but it's work perfect too add raw html code in a textarea.

诀窍是用 span 标签包裹你的 textarea 以帮助 replaceWith 函数。我不确定它是否很干净,但是在 textarea 中添加原始 html 代码也很完美。