问题
I have to allow the user to enter carriage returns in text areas; something like:
Sentence 1
Sentence 2
...
I have to persist those carriage returns when loading and saving data. I use jQuery on the client side, and .NET on the server. Any suggestions on how to approach?
Thanks.
回答1:
If by "persist" the line breaks (CRLF) you mean that you want to display it properly, as SO does, you need to remeber to replace the CRLF pair with <br/>CRLF.
Otherwise, all your text will appear sequentially.
回答2:
You don't need to do anything special. That's what <textarea>s do, and unless you make a specific effort to strip out the newlines on the server side you'll load the save them with a standard string no problem.
回答3:
Use jQuery .val() hook:
$.valHooks.textarea = {
get: function( elem ) {
return elem.value.replace( /\r?\n/g, "\r\n" );
}
};
来源:https://stackoverflow.com/questions/1036185/carriage-return-in-text-area