如何将数据从 .txt 文件加载到文本区域
How to load data from a .txt file into a textarea
我正在尝试用 .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',''));
.fillText
表示您要查找 HTML 元素,其中 class="fillText"
#fillText
表示您想要查找 HTML 元素,其中 id="fillText"
,这似乎是您想要的。
你可能想通过这个:
http://www.w3schools.com/cssref/css_selectors.asp
您也可以使用您在 #
中展示的加载函数,因为它正是为此而设计的。
我正在尝试用 .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',''));
.fillText
表示您要查找 HTML 元素,其中 class="fillText"
#fillText
表示您想要查找 HTML 元素,其中 id="fillText"
,这似乎是您想要的。
你可能想通过这个: http://www.w3schools.com/cssref/css_selectors.asp
您也可以使用您在 #
中展示的加载函数,因为它正是为此而设计的。