繁体   English   中英

如何将.txt文件中的数据加载到textarea中

[英]How to load data from a .txt file into a textarea

我正在尝试使用.txt文件中的数据填充textarea。 我尝试过使用类似问题的答案,但我找不到有效的答案。
在文件的头部,我有这个导入一个与jQuery一起使用的库:

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

Textarea声明:

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

这是我在textarea声明之后的脚本标记:

<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>

我也尝试过使用它:

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

但由于某种原因,这不起作用。

由于您使用的是textarea ,因此需要指定值而不是html。

更改:

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

至:

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

.fillText表示您要查找其中class="fillText"的HTML元素
#fillText意味着你想要找到一个id="fillText"的HTML元素,这就是你想要的。

你可能想要这样做: http//www.w3schools.com/cssref/css_selectors.asp

你也可以使用你在#显示的加载函数,因为它就是这样的。

暂无
暂无

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

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