我有一个 javascript 变量,它显示在我的 html 网站上,我还有一个 .txt 文件,它从 .php 脚本获取付款值。这些值在 .txt 文件中显示为 12345,每个金额加 1,因此 .txt 文件首先获取 1,然后 2,然后 3 等。现在我的问题是,如何从 .txt 获取最后一个值文件并将我的变量值转换为我得到的值,通过 .txt 文件的最后一个值我的意思是如果文件中的当前值是 12345 那么最后一个值是 5,那么我想将 Javascript 中的变量更改为该值 5。
我的 javascript/html 脚本,其中包含变量并显示。
<div class="newValue">
Current value: $<span id="newValueVariable"></span>
</div>
<script type="text/javascript">
var newValueVariable= 1;
document.getElementById("newValueVariable").innerHTML = newValueVariable;
</script>
最佳答案
使用 AJAX:
var xhr = new XMLHttpRequest;
xhr.open("filename.txt", "GET", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var contents = xhr.responseString;
// Replace next line with what you actually use to parse the file
var lastChar = contents.substr(-1, 1);
document.getElementById("newValueVariable").innerHTML = lastChar;
}
}
xhr.send();
关于javascript - 从 JavaScript 中的文本文件获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441097/