[英]Save options from a select to file with JS
您好,我有一个选择和一个输入按钮来浏览文件,我希望将选择中的选项保存为格式为文本文件:
线路 1 线路 2 线路 3 ...
我已阅读本教程:
但这似乎不起作用。 当我保存时,选项被写入同一行,并且只有选项值的第一个单词被写入文件。 IE 我的选择是:
买食物打电话给约翰打印文件
文件中的输出是:buy call print
我使用的代码是:
var textToWrite = "";
$('#todolist>option').each(function () {
textToWrite += this.value + "\n";
});
var textFileAsBlob = new Blob([textToWrite], {type: 'text/plain'});
var fileNameToSaveAs = "directive.txt";
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
console.log("innerHTML -> " + downloadLink.innerHTML);
window.webkitURL != null;
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
downloadLink.click();
}
var button = document.getElementById('file-save');
button.addEventListener('click', saveTextAsFile);
我正在使用 google 的 jquery 链接:
有人可以帮忙吗? 提前致谢
因此,您还想要文本,而不仅仅是值?
$('#todolist>option').each(function () {
textToWrite += this.value + "\n" + this.innerText + "\n";
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.