A solution for failing to wrap the content submitted by textarea

Source: Internet
Author: User

When we submit data through textarea on the page, the content we input will be processed as a piece of content after submission, even if we input a lot of line breaks in textarea, after a piece of content is submitted, what should we do if we need to save the input information as input?

The main problem is that the line break is the <br/> label when the page is displayed, and the line break when the content is displayed in textarea is \ n, the following describes a method to convert a line break in the input content to a line break in the webpage through js. You can simply submit the money on the webpage for the following processing.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Press enter to convert the line
Var content = document. getElementById ("content"). value;
Content = content. replace ('\ n',' <br/> ');
Document. getElementById ("content"). value = content;
// Then submit
</Script>

When displayed on the page, you must perform the following operations:

Copy codeThe Code is as follows:
Content = content. replace ('<br/>', '\ n ');

The following describes the replace function of javascript:

Syntax:

StringObject. replace (regexp/substr, replacement)

The replace () method is used to replace other characters with some characters in a string, or replace a substring that matches a regular expression.

1. Replace a single character

Copy codeThe Code is as follows:
<Script language = "javascript">
Var str = "javascript is a good script language ";
Alert (str. replace ("a", "A"); // output jAvascript is a good script language
</Script>

2. replace all specified characters in the string (replace all)

Copy codeThe Code is as follows:
<Script language = "javascript">
Var str = "javascript is a good script language ";
Alert (str. replace (/a/g, "A"); // output jAvAscript is A good script lAnguAge
</Script>

The above/g is a regular expression, which indicates that you can also use a regular expression in replace.

Of course, replace has more advanced usage. You can get more advanced usage in related documents.


Related Article

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.