用JS从选择中保存选项

发布于 2025-02-10 12:24:07 字数 1196 浏览 2 评论 0原文

您好,我有一个选择和一个输入按钮来浏览文件,我很想将选项保存到“选择”中的文本文件中:

line1 LINE2 线3 ...

我已经阅读了本教程:

save< select oflect;文本文件

但似乎不起作用。保存时,选项写在同一行中,只有第一个单词的选项值才会贴在文件上。 即我的选择是:

购买食物 打电话给约翰 打印文档

文件中的输出是: 购买

我使用的代码是:

       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:

有人可以提供帮助吗?提前致谢

Hello I have a select and an input button to browse files, and i would love to save the options in the select to a text file in the format:

line1
line2
line3
...

I have read this tutorial:

Save <Select >Tag values into text file

But it does not seem to work. When i save, the options are written in the same line and only the first word for the option value is writte to the file.
I.E. my options are:

buy food
call john
print documents

the output in the file is:
buy call print

The code i use is:

       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);

I am using google's link for jquery:

Can anybody help? Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2025-02-17 12:24:07

因此,您还想要文本,不仅要值值?

$('#todolist>option').each(function () {
    textToWrite += this.value + "\n" + this.innerText + "\n";
});

So you want also the text, not only the values?

$('#todolist>option').each(function () {
    textToWrite += this.value + "\n" + this.innerText + "\n";
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文