我正在尝试用 .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/