[英]Can't Append strings from txt file
我正在尝试读取一个名为start.txt的本地文件
如果我到达第一行是nextpage
,它将执行某些操作,但如果不是,它将附加到文本区域。 但是它给我一个错误:
Uncaught TypeError: Cannot read property 'inputtext' of undefined (23:44:44:520 | error, javascript) at (public_html/index.html:12:50)
我尝试读取的文件与index.html文件位于同一文件夹中。 我对HTML,AJAX和JavaScript相当陌生,所以我可能只是不知道自己在做什么!
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<script type="text/javascript">
var txtFile = new XMLHttpRequest();
var inputarea = document.textareaform.inputtext.value;
txtFile.open("GET", "http://localhost/start.txt", true);
txtFile.onreadystatechange = function() {
// Makes sure the document is ready to parse.
if(txtFile.readyState === 4) {
// Makes sure it's found the file.
if(txtFile.status === 200) {
allText = txtFile.responseText;
// Will separate each line into an array
lines = txtFile.responseText.split("\n");
for(i = 0; i < lines.length; i++) {
var s = lines[i];
if(s.indexOf("nextpage") > -1) {
// Line is there
} else {
// Line is not there
inputarea += s;
}
}
}
}
};
txtfile.send(null);
</script>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form name="textareaform">
<textarea name="fileoutput" rows="4" cols="20" readonly="readonly">
</textarea>
</form>
<div>TODO write content</div>
</body>
</html>
它正在寻找一个名为“ inputtext”的textarea
,并且已将其命名为“ fileoutput”。
尝试将文本区域重命名为“ inputtext”:
<textarea name="inputtext" rows="4" cols="20" readonly="readonly"></textarea>
要么
分别定义变量:
var inputarea = document.textareaform.fileoutput.value;
将var inputarea声明更改为此。
var inputArea = document.getElementsByName('fileoutput');
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.