将文本文件加载到HTML中的正确文本框中
我一直在玩这个代码,尚未弄清楚如何做。该代码为JavaScript,将单个文本框值保存到文本文件中,以后可以将其加载到文本框中。问题是我试图使其与网站上的多个文本框一起使用,但它要么仅对一个文本框来,要么无法将文本文件中的信息分开,而只是将所有框中的所有相同值都放入每个文本中盒子。
关于我应该采取的措施使这项工作用单独的文本框进行操作,并将正确的信息加载到正确的盒子中吗?谢谢
<html>
<body>
<table>
<tr><td>Text to Save:</td></tr>
<tr>
<td colspan="3">
<textarea id="inputTextToSave" cols="80" rows="25">
</textarea>
</td>
</tr>
<tr>
<td>Filename to Save As:</td>
<td><input id="inputFileNameToSaveAs"></input>
</td>
<td><button onclick="saveTextAsFile()">Save Text to File</button></td>
</tr>
<tr>
<td>Select a File to Load:</td>
<td><input type="file" id="fileToLoad"></td>
<td><button onclick="loadFileAsText()">Load Selected File</button><td>
</tr>
</table>
<script type="text/javascript">
function saveTextAsFile()
{
var textToSave =
document.getElementById("inputTextToSave").value;
var textToSaveAsBlob = new Blob([textToSave],
{type:"text/plain"});
var textToSaveAsURL =
window.URL.createObjectURL(textToSaveAsBlob);
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
function loadFileAsText()
{
var fileToLoad =
document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded =
fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
</script>
</body>
</html>
I have been playing with this code and have yet to figure out how to do it. The code is javascript and will save a single textbox value into a text file that can later be loaded back into the textbox. The problem is that I am trying to make it work with multiple text boxes on a website but it either just works on one or it can not separate the information in the text file and just put all the same values from all the boxes into each text box.
Any idea on what I should do to make this work with seperate textboxes and load the correct info into the correct box? thanks
<html>
<body>
<table>
<tr><td>Text to Save:</td></tr>
<tr>
<td colspan="3">
<textarea id="inputTextToSave" cols="80" rows="25">
</textarea>
</td>
</tr>
<tr>
<td>Filename to Save As:</td>
<td><input id="inputFileNameToSaveAs"></input>
</td>
<td><button onclick="saveTextAsFile()">Save Text to File</button></td>
</tr>
<tr>
<td>Select a File to Load:</td>
<td><input type="file" id="fileToLoad"></td>
<td><button onclick="loadFileAsText()">Load Selected File</button><td>
</tr>
</table>
<script type="text/javascript">
function saveTextAsFile()
{
var textToSave =
document.getElementById("inputTextToSave").value;
var textToSaveAsBlob = new Blob([textToSave],
{type:"text/plain"});
var textToSaveAsURL =
window.URL.createObjectURL(textToSaveAsBlob);
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
function loadFileAsText()
{
var fileToLoad =
document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded =
fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否是您要找的,但这里是:
这里我添加第二个 TextArea 并更改类的 id 以按 className 选择所有 TextArea。
这个想法是将所有文本区域的文本连接到一个字符串中,然后将它们分开。为此,我使用分隔符。您必须使用一些您知道不会出现在文本中的分隔符。通常,像 ¶(十六进制的 0xB6)这样的不可打印字符可能很有用。
有了这个想法,我使用 getAllTextBoxesText 函数将所有文本区域的文本连接到一个字符串中。这是使用原始保存功能保存的文本。
对于加载部分,我们执行相同的操作:splitTextBox 函数仅使用分隔符分割文本,并将每一部分设置在一个文本区域中。在获取加载的文本内容后,我在 loadFileAsText 中使用此函数。
您可以在这里测试: https://jsfiddle.net/pyv5djbe/
I'm not sure if this is what you're looking for, but here goes:
Here I add a second TextArea and change id for class to select all TextArea by className.
The idea is join all textarea's texts in one string and later, split them. To do that, I use a delimiter. You must use some delimiter that you know won't appear in your text. Usually, a non printable character like ¶ (0xB6 in hexadecimal) may be useful.
With this idea, I use getAllTextBoxesText function to join all textarea's texts in a single string. This is the text to save with your original save function.
For the load part, we do the same: splitTextBox function only splits the text using the delimiter and set each part in one textarea. I use this function in your loadFileAsText, after get the loaded text content.
You can test here: https://jsfiddle.net/pyv5djbe/