最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - append text to a textarea with line breaks - Stack Overflow

programmeradmin0浏览0评论

I want to add some strings to a textarea which are file basenames. Everything is fine, but the only problem is that it mixes all the values and there are not any line breaks:

var file_name = file.file_name;
var base = new String(file_name).substring(file_name.lastIndexOf('/') + 1); 
if(base.lastIndexOf(".") != -1)       
base = base.substring(0, base.lastIndexOf("."));
$('textarea#image_Basename').append(base).split('\n');

These are my file basenames:

5b0cd65710052633dc5dcac406a382c4
212asaddgcvjh622sdsds22113554dfd
5sd5weea55rr6qasfdjkloijhj665s6a

But after storing the data in to the database and retrieving it, the result I get is:

5b0cd65710052633dc5dcac406a382c4212asaddgcvjh622sdsds22113554dfd5sd5weea55rr6qasfdjkloijhj665s6a

I want to add some strings to a textarea which are file basenames. Everything is fine, but the only problem is that it mixes all the values and there are not any line breaks:

var file_name = file.file_name;
var base = new String(file_name).substring(file_name.lastIndexOf('/') + 1); 
if(base.lastIndexOf(".") != -1)       
base = base.substring(0, base.lastIndexOf("."));
$('textarea#image_Basename').append(base).split('\n');

These are my file basenames:

5b0cd65710052633dc5dcac406a382c4
212asaddgcvjh622sdsds22113554dfd
5sd5weea55rr6qasfdjkloijhj665s6a

But after storing the data in to the database and retrieving it, the result I get is:

5b0cd65710052633dc5dcac406a382c4212asaddgcvjh622sdsds22113554dfd5sd5weea55rr6qasfdjkloijhj665s6a
Share edited May 27, 2015 at 4:28 Sebas 21.5k9 gold badges59 silver badges109 bronze badges asked Aug 16, 2013 at 17:09 AfshinAfshin 2,4375 gold badges37 silver badges57 bronze badges 7
  • 1 Can you provide an example of the content of file_name as it is before manipulating it? – Teemu Commented Aug 16, 2013 at 17:23
  • I'm uploading the file and I use this on uploadsuccess: 'onUploadSuccess' : function(data, file, response) {file = JSON.parse(file);var file_name = file.file_name; and the file name is like: file_name = 5b0cd65710052633dc5dcac406a382c4.jpg – Afshin Commented Aug 16, 2013 at 17:26
  • I've updated my answer to support two ways of dealing with the problem. – rink.attendant.6 Commented Aug 16, 2013 at 17:31
  • There seems not to be / characters, nor \ns in the file_name, also there's only one file name... – Teemu Commented Aug 16, 2013 at 17:33
  • @Teemu This function might be called multiple times, in which case there would be multiple filenames being appended, like the three above. – rink.attendant.6 Commented Aug 16, 2013 at 17:35
 |  Show 2 more ments

2 Answers 2

Reset to default 3

To preserve newlines that are ing from a database or whatever, replace the newline characters with the HTML entity for a line feed: 


base = base.replace("\n", '
');
$('#image_Basename').append(base);

If you're trying to append each string with a newline at the end, just concatenate it onto the string:

$('#image_Basename').append(base + '
');

Also, you're using split on the textarea jQuery element, which doesn't make sense as it is an object not a string.

My Special thanks to @rink.attendant.6, his second method worked for me :) The answer is:

$('#image_Basename').append(base + '
');

After adding this, I got all the file basenames in separate lines!

发布评论

评论列表(0)

  1. 暂无评论