我有一个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>
改变
document.getElementById("demo").innerHTML = xhttp.responseText;
至
document.getElementById("demo").value = xhttp.responseText;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句