2017-10-07 175 views
0

有5个textarea。从这个1问题4回答如何将数据从textarea的文件写入到txt文件

<!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 = “问题” 的问题 ID = “答案*” 答案 答案可以是两个或三个或四个

对于添加chekbox的答案,这个复选框需要修复正确的答案 如果复选框被屏蔽了,我希望点击类中的保存按钮后添加class“CORRECT”

有按钮“保存”

我需要脚本中保存从文本域数据为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>

我希望我解释什么,我想做的事情。 ..

在年底,所以我就:有数据

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> 

等...等...

回答

0

使用JavaScript(在客户端)无法将数据保存到文件中。你需要做的是创建一个保存信息的服务器,并从网页向它发出请求。

1

$("#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>

Saving file with pure js

的引用