Show line Wrap in textarea

Source: Internet
Author: User
TextArea Line Change

TextArea is saved to the MySQL database when using inline editing in the DataGrid in Easyui \ n
Enter a return character in the textarea the value in the JS read textarea has \ r \ n then to the business layer conversion to string is likely to become a space form and then be stored in the database, when this value is taken out of the form of space, so we need to not show the \ r \ n Replace.

When JS takes the textarea:

If you use

var str = document.getelementbyidx_x_x ("textarea"). Value;
str = str.replace ("\ r \ n", "<br>");

Only the first \ r \ n is replaced with <br>, how to replace all n \ r \ NAND <br>. Do you want to have an unlimited replace?

In fact, replace can be combined with regular expressions, replacing all \ r \ n at once.

var reg=new RegExp ("\ r \ n", "G");
Str= str.replace (Reg, "\ r \ n");

So all of the \ r \ n in STR is replaced with <br>. You can then save it to the database.

You can only replace the value when you remove it.

var reg=new RegExp ("<br>", "G");
stt= str.replace (Reg, "\ r \ n");
Document.getelementbyidx_x_x ("textarea"). Value=str;

More detailed analysis and application can refer to the JavaScript of the Replace method and regular expressions combined with the application of the explanation

Here are 2 functions I wrote directly into the generic package for later use to replace

function ReplaceTextarea1 (str) {
var reg=new RegExp ("\ r \ n", "G");
var reg1=new RegExp ("", "G");

str = str.replace (Reg, "<br>");
str = str.replace (REG1, "<p>");

return str;
}


function ReplaceTextarea2 (str) {
var reg=new RegExp ("<br>", "G");
var reg1=new RegExp ("<p>", "G");

str = str.replace (Reg, "\ r \ n");
str = str.replace (REG1, "");

return str;
}

2. HTML in-Pass

The content submitted by the label will turn the carriage return to "\ r \ n", and then put the submitted content into

When displayed in the label, the carriage return reverts to the normal line break. However, when a string with "\ r \ n" is displayed in the normal text field of the HTML, the original wrapping effect is changed to a space, so you need to replace "\ r \ n" in the string with ". In addition, it is important to note that the El expression converts the newline character to "," < ">" correspond to "<" and ">" respectively, that is to say, on the page, or show as "" (Hehe, El Intelligent degree is too high), in order to remedy this, You need to use JSP tags (


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.