Example of a replacement for newline characters in a text field

  • 2020-03-30 02:13:07
  • OfStack

Note: the code passed the test in the actual project, I have omitted some unimportant, you can focus on the part of the color marking


Q: why did you do this?

A: there is a project to save the input text field text to the database. 'when view' retrieves the data from the database and displays it in the text field. During the development, we found that the val() or text() of jquery is not processed after it is acquired. There is no new line in the saved database, so there will be no new line when it is displayed naturally. Hence the following article... A bit wordy!
<%@ page contentType="text/html;charset=UTF-8" %> 
<script type="text/javascript"> 
//Text field newline processing
//When initializing, the data returned by the background will be processed by lines, and the @@@@ will be replaced by the newline character nr
var content = ' The value obtained from the background '; 
if(content !='' ){ 
//Global replacement
content = content.replace(/@@@@/g,'nr'); 

//Replace the newline character nr with @@@@ when committing to save
function doSubmit() { 
var content = $("#content").val().replace(/n/g,"@@@@"); 
if(content == null || content==""){ 
alert(" Please fill in the remarks! "); 
$("#updateForm").attr("action", url); 
<div class="layout-bd"> 
<div class="wrapper fix-float"> 
<div class="se se-e"> 

<div class=".layout-ft .copyright"><p><font face=" Chinese travel model " size="10"> Interview plan </font></p> 
<div>  plan <br><hr></div> 
<div > 
<textarea id="content" rows="15" cols="50"></textarea> 
<div > 
<a id="submit_btn" class="btn btn-save" onclick="doSubmit();"> save </a> 
<form id="updateForm" method="get"> 

Related articles: