gpt4 book ai didi

javascript - 将 .txt 文件加载到 textarea Javascript?

转载 作者:太空狗 更新时间:2023-10-29 13:59:09 25 4
gpt4 key购买 nike

我试图将文本文件放入文本区域。结果是“http://mywebsite.com/textfile/(txtinput).txt,文本文件没有加载到文本区域。

<html>
<head>
<title>textbox</title>
<script type="text/javascript">
function readBOX() {
var txtinput = document.getElementById('txtinput').value;
document.forms[0].text.value = ("http://mywebsite.com/textfile/") + txtinput +(".txt");
}
</script>
</head>
<body>
<p> Type</p>
<input type="text" id="txtinput" />
<input id="open" type="button" value="READ" onClick="readBOX()" />
<form>
<textarea name="text" rows="20" cols="70">loaded text here</textarea>
</form>
</body>
</html>

最佳答案

您必须使用类似于 this Answer 中发布的内容

jQuery
$(document).ready(function() {
$("#open").click(function() {
$.ajax({
url : "helloworld.txt",
dataType: "text",
success : function (data) {
$("#text").text(data);
}
});
});
});

阅读有关 jQuery Documentation of .ajax() 的更多信息

非 jQuery

如果你不想使用 jQuery,你必须使用 XMLHttpRequest-Object something like that:

var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;

但这可以在 SO-Answer 上阅读 here或关于 Wikipedia 的完整且易于理解的文档

注意:但这不是跨浏览器兼容的,对于较旧的 IE 版本,您必须使用 ActiveXObject("Microsoft.XMLHTTP") 对象

关于javascript - 将 .txt 文件加载到 textarea Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19020484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com