The value and value assignment of JQuery TextArea

Source: Internet
Author: User

First, it is not a simple problem!

Values:

$ ("# Textarea"). text (); // read
In recent tests, we found that textArea values cannot be read in firefox or Chrome, and IE is normal.

Therefore, it is changed:

$("#textarea").val();
Solve the problem. However, when searching, someone found another problem:

When you type a carriage return in a textarea tag, two symbols are inserted: \ n \ r \ n carriage return \ r linefeed.

If you use text () to obtain the value in textarea, the data stored in the database will only have \ r. If you use var () to obtain the value in textarea,
The difference between \ n and the data stored in the database is that in IE,
If you read the data stored in text () and are displayed in textarea, the line feed of the data is normal.
The data stored in var () is read and displayed in textarea, so there is no line feed for the data.
It does not seem appropriate to use val () without a specific test. But this is also a required strategy.

Value assignment:
Check the Code:

$("" + "").appendTo("body");$("#t1").text("AAA");$("#t2").val("BBB");alert("t1=" + $("#t1").text() + "/" + $("#t1").val());alert("t2=" + $("#t2").text() + "/" + $("#t2").val());alert($("#x1").html());
In IE, no matter whether set with val () or text (), subsequent reads will be normal. However, in Firefox, the image will appear with the value specified by val, however, text () or html () is a string. Therefore, in Firefox, if you want to set the textarea value to appear in html () or can be cloned (), use text.

And slow. If things are so simple, even elementary school students will be cross-browser. Note the line feed when using text () settings. If you use text ("A \ nA") in IE, only an empty grid is displayed, but no line feed is displayed. According to the test results, text ("A \ r \ A") in IE is close to expectation, but text ("A \ rA ") columns will be changed when displayed in Firefox, but are connected when retrieved using text... this... this... this...
Finally, we can only adopt the cowardly policy. We did this before calling. clone () to avoid the problem:
$theDiv.find("textarea").each(function() { $(this).text($(this).val()); }); 
Ugly, But it seems effective! If someone has other good ideas, share them with me!
[Tips] You are not capable of being a trojan. Don't tell others that you will "Cross" browsers!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.