On the value and assignment of JQuery textarea

Source: Internet
Author: User

First, it's not a simple question.

First, take the value:

$ ("#textarea"). Text ();//Read
The latest test found in Firefox or Chrome read textarea value, ie normal.

It is then modified to:

$ ("#textarea"). Val ();
Problem solved, but when searching, it was found that someone asked another question:

When you type a carriage return in a textarea tab, you actually insert 2 symbols: \n\r \ r \ n line break

If you use text () to get the value in textarea, then the data stored in the database will have only \ r if you use VAR () to get the value in textarea,
Then the data stored in the database will only have the difference is that, under IE,
If you are reading the data stored in the text () in the display in the textarea, then the line of data is normal.
While reading data stored in VAR () is displayed in the textarea, the data is not wrapped.
It is not appropriate to use Val () without a specific test. But this is also a must to seek the policy.

Besides, the assignment:
Look at the code:

$ ("<div id= ' x1 ' ><textarea id= ' t1 ' ></textarea>" +
 "<textarea id= ' T2 ' ></textarea> </div> "). 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, the subsequent reads are normal, regardless of the Val () or text (), but in Firefox, the value specified by Val () appears, but the text () or the HTML () view is the string. So in Firefox, if you want to set the textarea value to appear in HTML () or can be clone (), use text (...). Set.

Wait. If things are so simple, even primary school students will cross the browser. Use text () settings should pay attention to the problem of line, if you are in IE next text ("A\na"), in the display will only render empty one and will not wrap. According to my test results, in IE under the text ("A\r\a") to show the results are relatively close to the expected, but text ("A\ra") in Firefox will be displayed in the column, with text () when it is connected together ... This... This... This...
Finally, you can only take a cowardly strategy and do this before calling. Clone ():
Very ugly, but it seems to be effective! If someone has another good idea, share it.
"Experience" does not have the ability to be a horse, do not tell others you will "cross" browser.

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.