Carriage return in text area

我是研究僧i 提交于 2019-12-08 03:28:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!