简体   繁体   English

如何使用.val()将textarea换行符插入生成的文件

[英]How to get textarea line breaks into generated file with .val()

I would like to download content of textarea into file but also remain linebreaks. 我想将textarea的内容下载到文件中,但也要保留换行符。

I was doing it with .html() function, but then I needed to be able to edit the textarea, so I used .val() to insert values to textarea. 我当时使用.html()函数来完成此操作,但是随后我需要能够编辑文本区域,因此我使用.val()将值插入到文本区域中。 It worked fine, but after the change .html() doesn't read the content, only val() does. 它工作正常,但是更改后.html()不读取内容,只有val()可以。 But the file that it downloads doesn't contain linebreaks that are necessary for the purpose. 但是下载的文件不包含为此目的所需的换行符。

Content of textarea is being set through for-loop with adding "\\n" at each line end. 通过for循环在每个行末添加“ \\ n”来设置textarea的内容。

I am using filerSaver.js for downloading files 我正在使用filerSaver.js下载文件

var content = $( '#cdr_container' ).val();

var blob = new Blob([content], {
    type: "text/plain;charset=utf-8"
});

saveAs(blob, filename);

I need to get those linebreaks into file as well. 我还需要将这些换行符记录到文件中。 Is there any work around? 有什么解决办法吗? Thank you for help. 谢谢你的帮助。

Solved by a simply adding this in front of text declaration 通过在文本声明前面简单添加此内容即可解决

$.valHooks.textarea = {
    get: function( elem ) {
        return elem.value.replace( /\r?\n/g, "\r\n" );
    }
};

Here is the issue described 是描述的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM