简体   繁体   English

我可以使用 javascript 将 HTML 表单数据(包括单选按钮)保存为本地 txt 文件吗?

[英]Can I save a HTML form data (including radio button) as a local txt file using javascript?

The code below works perfectly to download a text box.下面的代码非常适合下载文本框。 However, I wish to expand upon it to add three boxes and a radio button.但是,我希望扩展它以添加三个框和一个单选按钮。 I'd then like to be able to download the .txt containing all the data.然后,我希望能够下载包含所有数据的 .txt。 For example, FirstName, Surname, Email, Male/Female(radio button).例如,名字、姓氏、电子邮件、男/女(单选按钮)。 I can't seem to find a way of doing this.我似乎找不到这样做的方法。 Any help hugely appreciated!非常感谢任何帮助!

 <!DOCTYPE html> <html> <head> <style> form * { display: block; margin: 10px; } </style> <script language="Javascript" > function download(filename, text) { var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); pom.setAttribute('download', filename); pom.style.display = 'none'; document.body.appendChild(pom); pom.click(); document.body.removeChild(pom); } </script> </head> <body> <form onsubmit="download(this['name'].value, this['text'].value)"> <input type="text" name="name" value="test.txt"> <textarea rows=3 cols=50 name="text">Please type in this box. When you click the Download button, the contents of this box will be downloaded to your machine at the location you specify. Pretty nifty. </textarea> <input type="submit" value="Download"> </form> </body>

You can get the values with getElementById if you set id's on each element as the pattern I'll did.如果您将每个元素的 id 设置为我将做的模式,则可以使用 getElementById 获取值。 Join them together with a line brake.用线制动器将它们连接在一起。

 <!DOCTYPE html> <html> <head> <style> form * { display: block; margin: 10px; } </style> <script language="Javascript" > function download(filename) { var inputArray = []; inputArray[0]='Firstname: '+document.getElementById('firstName').value; inputArray[1]='Lastname: '+document.getElementById('lastName').value; inputArray[2]='Text 1: '+document.getElementById('text1').value; inputArray[3]='Text 2: '+document.getElementById('text2').value; var text = inputArray.join('\n'); var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); pom.setAttribute('download', filename); pom.style.display = 'none'; document.body.appendChild(pom); pom.click(); document.body.removeChild(pom); } </script> </head> <body> <form onsubmit="download(this['name'].value)"> <input type="text" name="name" value="test.txt"> <input type="text" id="firstName" placeholder="Firstname"> <input type="text" id="lastName" placeholder="Lastname"> <textarea rows=3 cols=50 id="text1"> First </textarea> <textarea rows=3 cols=50 id="text2"> Second </textarea> <input type="submit" value="Download"> </form> </body>

I think it is better to generate output content as JSON. That make data more readable.

 <!DOCTYPE html> <html> <head> <style> form * { display: block; margin: 10px; } </style> <script language="Javascript"> function download() { var text = document.getElementsByName('text')[0].value; var gender = document.getElementsByName('gender')[0].value; var email = document.getElementsByName('email')[0].value; var fileName = document.getElementsByName('name')[0].value; var obj = { text: text, gender: gender, email: email }; var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(obj))); pom.setAttribute('download', fileName); pom.style.display = 'none'; document.body.appendChild(pom); pom.click(); document.body.removeChild(pom); return false; } </script> </head> <body> <form onsubmit="return download()"> <input type="text" name="name" value="test.txt"> <textarea rows=3 cols=50 name="text">Please type in this box. When you click the Download button, the contents of this box will be downloaded to your machine at the location you specify. Pretty nifty. </textarea> <input type="text" name="email" id="email"> <input type="radio" name="gender" id="gender" value="male"> Male <input type="radio" name="gender" id="gender" value="female"> Female <input type="submit" value="Download"> </form> </body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 JAVASCRIPT/jQuery 将表单中的输入保存到 HTML 中的 .txt,然后使用它吗? - Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it? 使用JavaScript将HTML表单数据保存到文件中 - Save HTML Form data into a File using JavaScript 如何使用javascript将表单数据附加并保存到txt文件 - How to append and save form data to txt file using javascript 如何将数据从.txt文件保存到JavaScript中的数组? - How can i save data from a .txt file to an array in JavaScript? 是否可以使用 html 上的输入值保存文本并使用 JavaScript 写入本地 txt 文件? - Is it possible to save texts from input value on html and write on local txt file by using JavaScript? 使用HTML,JavaScript将数据保存在本地文件中吗? - Save data in a local file with HTML, Javascript? 我可以从HTML表单中读取输入并将其保存在TXT文件中的特定位置吗? - Can I read the input from a HTML form and save it at at a specific position in TXT file? 我想使用JavaScript将文本框保存为HTML中的.txt文件,但出现错误 - I want to save textbox to .txt file in HTML using JavaScript but I have an error 将表单数据保存到文本文件 - HTML &amp; Javascript - Save form data to text file - HTML & Javascript 散景单击按钮使用 javascript 将小部件值保存到 txt 文件 - Bokeh click button to save widget values to txt file using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM