javascript - 如何将数据从 .txt 文件加载到文本区域

标签 javascript jquery html textarea text-files

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

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

文本区域声明:

<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',''));

关于javascript - 如何将数据从 .txt 文件加载到文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38230868/

相关文章:

javascript - 有什么方法可以使用 JavaScript *不*将焦点设置在任何 HTML 元素上?

javascript - 有什么方法可以查出 localForage/indexedDb 是否正在处理数据?

javascript - Jquery获取和设置html字符串某些部分的css属性

javascript - 具有特殊样式的背景 url

javascript - 使用 tablesorter,如何在应用排序后格式化列?

javascript - CryptoJS AES 加密输出长度

javascript - 检查是否使用 Javascript 加载了 jquery

javascript - 谷歌地点 ID 查找器

html - 哪一个最优化代码显示:none or visibility:hidden as per the performance?

javascript - 隐藏导航栏,同时在新窗口中打开