简体   繁体   中英

How to load data from a .txt file into a textarea

I am trying to fill a textarea with the data from a .txt file. I have tried using answers from similar questions but I can't find one that works.
In the head of the file I have this to import a library that is used with the jQuery:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Textarea declaration:

<textarea id="fillText" cols="40" rows="5"></textarea>

This is the script tag that I have right after the textarea declaration:

<script>
var fileName = '<?php echo $fileN;?>.txt'; //gets the filename
//The follow works and alerts the browser with the contents of the text file
jQuery.get(fileName, function(data) {
   alert(data);
   //process text file line by line
   $('fillText').html(data.replace('n',''));

});
</script>

I have tried using this also:

 $(".fillText").load(fileName);

But that does not work for some reason.

Since you are working with a textarea , you need to specify the value rather than the html.

Change:

$('fillText').html(data.replace('n',''));

To:

$('#fillText').val(data.replace('n',''));

.fillText means you want to look for an HTML element where class="fillText"
#fillText means you want to look for an HTML element where id="fillText" , which is what you seem to want.

you may wanna go though this: http://www.w3schools.com/cssref/css_selectors.asp

Also you can use the load function you showed with # since it is meant for exactly this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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