我有一个HTML文本区域,它读取文本文件,然后让用户编辑文本。我有一个“刷新”按钮,它使用AJAX重新加载它正在读取的文件到文本文件中。但是,我的问题是,当我在不按“保存”按钮的情况下向文本区域添加某些内容,然后按下“刷新”按钮,文本仍然停留在文本区域。
我希望发生的是,我添加的文本会被删除,因为AJAX代码将文本区域替换为文件内容。
以下是我的相关代码:
<script>
function reloadFile() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", "documents/test.txt", true);
xhttp.send();
}
</script>
<?php
$myfile = fopen($file, 'a+');
echo "<textarea id='demo'>";
// go through each line in the file, print its contents.
while(!feof($myfile)) {
echo fgets($myfile);
}
echo "</textarea>";
?>
<button type='button'>Update Changes</button>
<button type='button' onclick='reloadFile()'>Refresh</button>我的问题是,我能做些什么,以便当我按下刷新按钮时,没有实际保存在实际文件上的任何文本都会被丢弃?我试着把
document.getElementById("demo").value = "";在reloadFile函数的开头,但是这似乎没有做任何事情。
文件/test.txt:
<!doctype html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Header</h1>
</body>
</html>发布于 2016-03-29 01:21:37
变化
document.getElementById("demo").innerHTML = xhttp.responseText;至
document.getElementById("demo").value = xhttp.responseText;https://stackoverflow.com/questions/36273719
复制相似问题