如何使用html代码

时间:2016-07-11 07:07:47

标签: javascript html

我使用本网站上另一个线程的代码将表单数据保存到本地硬盘上的文本文件中。对于文本字段,它工作正常,但当我试图保存radiobutton的数据时,它没有发生我尝试了几种组合,但它仍然没有工作。 我使用的代码如下



<!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 WRITE ANSWER HERE. </textarea>
      <input type="radio" name="radio" value="Option 1" onclick="getElementById('problem').value=this.value;"> Option 1<br>
      <input type="radio" name="radio" value="Option 2" onclick="getElementById('problem').value=this.value;"> Option 2<br>
      <input type="text" name="problem" id="problem">
      <input type="submit" value="SAVE">
    </form>
  </body>
</html>
&#13;
&#13;
&#13;

我的电子邮件ID是 saurabhsrivastava37@gmail.com

1 个答案:

答案 0 :(得分:0)

那是因为您需要将此参数传递给函数download

&#13;
&#13;
<!DOCTYPE html>
<html>
  <head>
    <style>
      form * {
        display: block;
        margin: 10px;
      }
    </style>
    <script language="Javascript" >
      function download(filename, text, text1) {
        var pom = document.createElement('a');
        pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + 
                         encodeURIComponent(text + '\r\n' + text1));
        
        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, this['problem'].value)">
      <input type="text" name="name" value="test.txt">
      <textarea rows=3 cols=50 name="text">PLEASE WRITE ANSWER HERE. </textarea>
      <input type="radio" name="radio" value="Option 1" onclick="getElementById('problem').value=this.value;"> Option 1<br>
      <input type="radio" name="radio" value="Option 2" onclick="getElementById('problem').value=this.value;"> Option 2<br>
      <input type="text" name="problem" id="problem">
      <input type="submit" value="SAVE">
    </form>
  </body>
</html>
&#13;
&#13;
&#13;