可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am attempting to set a value in a textarea field using jquery with the following code:
$(\"textarea#ExampleMessage\").attr(\"value\", result.exampleMessage);
The issue is, once this code executes, it is not altering the text in the textarea?
However when performing an alert($(\"textarea#ExampleMessage\").attr(\"value\"))
the newly set value is returned?
回答1:
Have you tried val?
$(\"textarea#ExampleMessage\").val(result.exampleMessage);
回答2:
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 :)
回答3:
$(\"textarea#ExampleMessage\").val()
in jquery just a magic.
You should notice that textarea tag using inner html to display and not in value attribute just like input tag.
<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.
回答4:
I think this should work :
$(\"textarea#ExampleMessage\").val(result.exampleMessage);
回答5:
Oohh come on boys! it works just with
$(\'#your_textarea_id\').val(\'some_value\');
回答6:
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
$(\"textarea#ExampleMessage\").val(result.exampleMessage);
for
- IE8
- FF 3.6
- FF4
- Opera 11
- Chrome 10
回答7:
I had same issue and this solution didn\'t work but what worked was to use html
$(\'#your_textarea_id\').html(\'some_value\');
回答8:
On android .val
and .html
didn\'t work.
$(\'#id\').text(\"some value\")
did the job.
回答9:
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 :
$(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() );
I had problem with some special character ( & \' \" ) when they are between quot. But when I use the function :
$(textarea).html() the text is ok.
There an example form :
<FORM id=\"idFormContact\" name=\"nFormContact\" action=\"send.php\" method=\"post\" >
<FIELDSET id=\"idFieldContact\" class=\"CMainFieldset\">
<LEGEND>Test your newsletter» </LEGEND>
<p>Send to à : <input id=\'idInpMailList\' type=\'text\' value=\'youremail@gmail.com\' /></p>
<FIELDSET class=\"CChildFieldset\">
<LEGEND>Subject</LEGEND>
<LABEL for=\"idNomClient\" class=\"CInfoLabel\">Enter the subject: * </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();\"/>
<INPUT id=\"idBtnSend\" type=\"button\" value=\"Send\" onclick=\"onSend();\"/><BR/><BR/>
<LEGEND>Message</LEGEND>
<LABEL for=\"idTxtArGenHtml\" class=\"CInfoLabel\">Html code : * </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 :
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.
回答10:
Text Area doesnot have value. jQuery .html() works in this case
$(\"textarea#ExampleMessage\").html(result.exampleMessage);
回答11:
textarea doesn\'t store values as
<textarea value=\"someString\">
instead, it stores values in this format:
<textarea>someString</textarea>
So attr(\"value\",\"someString\")
gets you this result:
<textarea value=\"someString\">someOLDString</textarea>.
try $(\"#textareaid\").val()
or $(\"#textareaid\").innerHTML
instead.
回答12:
I tried with .val()
.text()
.html()
and had some bugs using jQuery to read or set value of a textarea... i endup using native js
$(\'#message\').blur(function() {
if (this.value == \'\') { this.value = msg_greeting; }
});
回答13:
It works for me....
I have built a face book wall...
Here is the basis of my code:
// SETS MY TEXT AREA TO EMPTY (NO VALUE)
$(\'textarea#message_wall\').val(\'\');
回答14:
We can either use .val() or .text() methods to set values. we need to put value inside val() like val(\"hello\").
$(document).ready(function () {
$(\"#submitbtn\").click(function () {
var inputVal = $(\"#inputText\").val();
$(\"#txtMessage\").val(inputVal);
});
});
Check example here: http://www.codegateway.com/2012/03/set-value-to-textarea-jquery.html
回答15:
To set textarea value of encoded HTML (to show as HTML) you should use .html( the_var )
but as mentioned if you try and set it again it may (and probably) will not work.
You can fix this by emptying the textarea .empty()
and then setting it again with .html( the_var )
Here\'s a working JSFiddle:
https://jsfiddle.net/w7b1thgw/2/
jQuery(function($){
$(\'.load_html\').click(function(){
var my_var = $(this).data(\'my_html\');
$(\'#dynamic_html\').html( my_var );
});
$(\'#clear_html\').click(function(){
$(\'#dynamic_html\').empty();
});
});
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script>
<textarea id=\"dynamic_html\"></textarea>
<a id=\"google_html\" class=\"load_html\" href=\"#\" data-my_html=\"<a href="google.com">google.com</a>\">Google HTML</a>
<a id=\"yahoo_html\" class=\"load_html\" href=\"#\" data-my_html=\"<a href="yahoo.com">yahoo.com</a>\">Yahoo HTML</a>
<a id=\"clear_html\" href=\"#\">Clear HTML</a>
回答16:
The accepted answer works for me, but only after I realized I had to execute my code after the page was finished loading. In this situation inline script didn\'t work, I guess because #my_form wasn\'t done loading yet.
$(document).ready(function() {
$(\"#my_form textarea\").val(\'\');
});
回答17:
You can even use the below snippet.
$(\"textarea#ExampleMessage\").append(result.exampleMessage);
回答18:
When I had JQuery v1.4.4 in the page, neither of these worked. When injecting JQuery v1.7.1 into my page, it worked finally. So in my case, it was my JQuery version that was causing the issue.
id ==> textareaid
======================
var script1 = document.createElement(\"script\");
script1.src = \"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\";
document.body.appendChild(script1);
var script2 = document.createElement(\"script\");
script2.type = \"text/javascript\";
script2.innerHTML = \"var $jq171 = $.noConflict();\";
document.body.appendChild(script2);
$jq171(\'#textareaid\').val(\'xxx\');
回答19:
Just use textarea Id by its type like it:
$(\"textarea#samplID\").val()
回答20:
Just use this code and you will always have the value:
var t = $(this);
var v = t.val() || t.html() || t.text();
So it will check val() and set its value. If val() gets an empty string, NULL, NaN o.s. it will check for html() and then for text()...
回答21:
This works:
var t = $(\'#taCommentSalesAdministration\');
t.val(\'Hi\');
Remember, the tricky part here is making sure you use the correct ID.
And before you use the ID make sure you put #
before it.
回答22:
Using $(\"textarea#ExampleMessage\").html(\'whatever you want to put here\');
can be a good way, because .val()
can have problems when you are using data from database.
For example:
A database field named as description
has the following value asjkdfklasdjf sjklñadf
. In this case using .val() to assign value to textarea can be a tedious job.
回答23:
I think there is missing one important aspect:
$(\'#some-text-area\').val(\'test\');
works only when there is an ID selector (#)
for class selector there is an option to use native value like:
$(\'.some-text-area\')[0].value = \'test\';