简体   繁体   English

如何将textarea中的数据写入txt文件

[英]How to write data from textarea`s in to txt file

Have 5 textarea. 有5个文本区域。 From this 1 for question, 4 for answers 从这个问题1到答案4

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> Question <br /> <textarea id="question" rows="4" cols="70"></textarea> <br /><br /><br /><br /> Answer <input id="answer1" type="checkbox" name="" value=""> <textarea rows="4" cols="70"></textarea> <br /><br /> Answer <input id="answer2" type="checkbox" name="" value=""> <textarea rows="4" cols="70"></textarea> <br /><br /> Answer <input id="answer3" type="checkbox" name="" value=""> <textarea rows="4" cols="70"></textarea> <br /><br /> Answer <input id="answer4" type="checkbox" name="" value=""> <textarea rows="4" cols="70"></textarea> <br /><br /> <button type="button">Save</button> </head> <body> </body> </html> 

id="question" for question id="answer*" for answers Answers can be two or three or four id =“ question”(问题)id =“ answer *”(答案)答案可以是两个或三个或四个

For answers added chekbox, this checkbox need for fix the correct answer If checkbox cheked I want that after clicking on the save button in the class add class "CORRECT" 对于添加了复选框的答案,此复选框需要修正正确的答案。如果复选框被选中,我希望在单击类中的“保存”按钮后添加类“ CORRECT”

Have button "Save" 有按钮“保存”

I need script for save data from textareas in to txt file In this form: 我需要将文本区域中的数据保存到txt文件中的脚本,格式如下:

 CODE <div id="seconddiv" class="centered"> <h2>Question</h2> <ul class="simpleList"> <li class="simpleListAnswer correct"> <span class="fa fa-square-o"></span> <span class="simpleListText">A. Correct Answer</span></li> <li class="simpleListAnswer"> <span class="fa fa-square-o"></span> <span class="simpleListText">B. Incorrect Answer</span></li> <li class="simpleListAnswer"> <span class="fa fa-square-o"></span> <span class="simpleListText">G. Incorrect Answer</span></li> <li class="simpleListAnswer"> <span class="fa fa-square-o"></span> <span class="simpleListText">D. Incorrect Answer</span></li> </ul> </div> 

I hope I explained what I want to do... 我希望我解释了我想做什么...

At the end so I got: 最后,我得到了:

TXT file with data: 带有数据的TXT文件:

   <div id="seconddiv" class="centered">
       <h2>Question 1</h2>
       <ul class="simpleList">

               <li class="simpleListAnswer correct"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">A. Correct Answer</span></li>

               <li class="simpleListAnswer"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">B. Incorrect Answer</span></li>

               <li class="simpleListAnswer"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">G. Incorrect Answer</span></li>

               <li class="simpleListAnswer"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">D. Incorrect Answer</span></li>   

       </ul>
   </div>

   <div id="seconddiv" class="centered">
       <h2>Question</h2>
       <ul class="simpleList">

               <li class="simpleListAnswer"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">A. Inorrect Answer</span></li>

               <li class="simpleListAnswer correct"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">B. Correct Answer</span></li>

       </ul>
   </div>

   <div id="seconddiv" class="centered">
       <h2>Question</h2>
       <ul class="simpleList">

               <li class="simpleListAnswer"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">B. Incorrect Answer</span></li>

               <li class="simpleListAnswer"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">G. Incorrect Answer</span></li>

               <li class="simpleListAnswer correct"> <span class="fa fa-square-o"></span>
               <span class="simpleListText">D. Correct Answer</span></li>   

       </ul>
   </div>   

Etc ... Etc ... 等等...等等...

 $("#btn-save").click( function() { var text = $("#textarea").val(); var filename = $("#input-fileName").val() var blob = new Blob([text], {type: "text/plain;charset=utf-8"}); saveAs(blob, filename+".txt"); }); 
 body { padding: 1em; background: #EEE; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/14082/FileSaver.js"></script> <form role="form"> <h3>Saving a file with pure JS!</h3> <p>Uses HTML5 W3C saveAs() function and the <a href="https://github.com/eligrey/FileSaver.js" target="_blank">FileSaver.js</a> polyfill for this.<br> I didn't think this was even possible without a server but the docs say it should work in IE 10+, Sweet!</p> <div class="form-group"> <label for="input-fileName">File name</label> <input type="text" class="form-control" id="input-fileName" value="textFile" placeholder="Enter file name"> </div> <div class="form-group"> <label for="textarea">Text</label> <textarea id="textarea" class="form-control" rows="10" placeholder="Enter text to save">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae iure ab voluptate sunt reiciendis, officia, aliquam temporibus. Quo laudantium quaerat ad, deleniti optio ex, dignissimos, ea accusamus placeat tempora minima!</textarea> </div> <button id="btn-save" type="submit" class="btn btn-primary">Save to file</button> </form> 

A reference from Saving file with pure js 使用纯js保存文件中的参考

You can't save data to files using javascript (on the client side). 您无法使用javascript(在客户端)将数据保存到文件中。 What you need to do is create a server that saves the information, and make a request to it from the web page. 您需要做的是创建一个保存信息的服务器,然后从网页上对其进行请求。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM