【问题标题】:Downloading html inputs to text file to C drive local folder将文本文件的 html 输入下载到 C 驱动器本地文件夹
【发布时间】:2019-03-02 10:48:06
【问题描述】:

我刚刚开始,我们学生和我创建了一个在线工作簿,但无法获得下载到文本文件的答案,它会下载,但是当我们打开文件时,它会出现 undefined 这是我的代码。

这适用于 Outlook,但学生没有 Outlook,他们有基于 Web 的 Office 365 电子邮件,我不允许使用我们的 smtp 服务器
电子邮件看起来像这样

姓名=此处显示的学生姓名
1.1= 答案显示在这里 1.2= 1.3= 1.4= 1.5= 1.6= 以此类推

这是我的代码示例

<form onsubmit="download(this['name'].value, ['text'].value, ['id'].value)">


<h4>Students Name<input type="text" name="Name" value="" size="50"><br></h4>
 <br>
<h4>1. Why is it important to think about safety?</h4>

<p><label for="q1"><input type="radio" name="1.1" value=" A" id="q1a" />it identifies where the risks are.</label></p>
<p><label for="q1"><input type="radio" name="1.1" value=" B" id="q1b" />because I may get hurt.</label></p>
<p><label for="q1"><input type="radio" name="1.1" value=" C" id="q1c" />because it may prevent accidents and keep everyone safe.</label></p>
<p><label for="q1"><input type="radio" name="1.1" value=" D" id="q1d"/>because it will keep others safe.</label></p>
<br>


<h4>11. Respirators should be used to prevent?</h4>
<input type="text" name="1.11" id="1.11" size= "120"></p>
<br>
<h4>12. Disposable gloves are optional but do provide a convenient way to avoid?</h4>

<input type="text" name="1.12" id="1.12" size= "120"></p>
<br>
<h4>13. Why should you prevent liquid oil and grease from entering the pores of your skin?</h4>

<input type="text" name="1.13" id="1.13" size= "120"></p>
<br>

<h4>14. Why shouldn't we use hot water to wash off grease and oil off our hands?</h4>


<input type="text" name="1.14" id="1.14" size= "120"></p>
<br>

<h4>15. List 3 things that may cause a fire or act as a fuel?</h4>
<p>a.   <input type="text" name="1.15a" id="1.15a" size= "117"></p>
<p>b.   <input type="text" name="1.15b" id="1.15b" size= "117"></p>
<p>c.   <input type="text" name="1.15c" id="1.15c" size= "117"></p>

  <input type="submit" value="Download">
  </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>

【问题讨论】:

  • 谢谢,正如您从我的问题中看到的那样,我对编码真的很陌生。我的学生已经想出了安全网页,可以在学校内部使用,并希望将输入数据保存到文本文件中。 Outlook 位是我可以使用表单 method=post action=mailto 将其通过电子邮件发送到 Outlook,但我的学生没有 Outlook 访问权限,因此我们希望在他们的计算机上另存为可下载的文本文件。
  • 这里有几个问题

    1.应该使用呼吸器来预防?


    2.一次性手套是可选的,但确实提供了一种方便的避免方式?


标签: html


【解决方案1】:

如果我正确理解您的问题,那么问题是您使用未定义的参数调用下载函数。 要从表单中获取数据,您可以遍历

document.getElementById('yourFrom').elements

并保护对象中的名称-值对。然后您可以将该对象传递给您的下载函数。

我的示例代码在函数中收集表单数据

getFormData()

通过单击按钮而不是提交表单来调用。

由于您的问题表单中有单选按钮,因此循环应该检查 它只保护选定的值。我将 cmets 放在我的示例代码中 解释这是如何完成的。

我把函数注释掉了

download()

因为我认为让这里的人下载文件不是一个好主意。 但是你可以看到,当你打开你的开发工具时,文件中的内容是安全的 浏览器。因此,我把这条线

console.log(...);

为方便起见,我还在代码 sn-p 中以表格形式放置了一些示例值。

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.setAttribute('target', new Date());
  pom.style.display = 'none';
  document.body.appendChild(pom);
  pom.click();
  document.body.removeChild(pom);*/
  console.log('filename: ' + filename);
  console.log('text: ' + text);
}

/* get the Data from questions form */
function getFormData() {
  var form = document.getElementById("questionsForm");
  var questions = form.elements;
  var ret_obj ={};
  var radios = [];
  for(var i = 0 ; i < questions.length ; i += 1){
    var item = questions.item(i);
    if (item.type == 'radio') {
      /* if question input type is radio */
      if (radios.indexOf(item.name) == -1) {
         /* safe radio group name in array radios
         to prevent check on other radios of the same group */
        radios.push(item.name);
        /* safe radio group name and checked value in ret_obj */
        ret_obj[item.name] = document.querySelector('input[name="' + item.name + '"]:checked').value;
      }
    } else {
      /* if question input is different from radio
         safe the name-value pair in ret_obj */
      ret_obj[item.name] = item.value;        }
    }
    console.log(JSON.stringify(ret_obj));
    download('yourFilename', JSON.stringify(ret_obj));
  }
<div>
<form id="questionsForm">

<h4>Students Name<input type="text" name="Name" value="TheStudentsName" size="50"></h4>

<h4>1. Why is it important to think about safety?</h4>
<p><label for="q1"><input type="radio" name="1.1" value="A" id="q1a" />it identifies where the risks are.</label></p>
<p><label for="q1"><input type="radio" name="1.1" value="B" id="q1b" checked/>because I may get hurt.</label></p>
<p><label for="q1"><input type="radio" name="1.1" value="C" id="q1c" />because it may prevent accidents and keep everyone safe.</label></p>
<p><label for="q1"><input type="radio" name="1.1" value="D" id="q1d"/>because it will keep others safe.</label></p>


<h4>11. Respirators should be used to prevent?</h4>
<p><input type="text" name="1.11" id="1.11" size= "120" value="answer11"></p>

<h4>12. Disposable gloves are optional but do provide a convenient way to avoid?</h4>
<p><input type="text" name="1.12" id="1.12" size= "120" value="answer12"></p>

<h4>13. Why should you prevent liquid oil and grease from entering the pores of your skin?</h4>
<p><input type="text" name="1.13" id="1.13" size= "120" value="answer13"></p>

<h4>14. Why shouldn't we use hot water to wash off grease and oil off our hands?</h4>
<p><input type="text" name="1.14" id="1.14" size= "120" value="answer14"></p>

<h4>15. List 3 things that may cause a fire or act as a fuel?</h4>
<p>a.   <input type="text" name="1.15a" id="1.15a" size= "117" value="answer15a"></p>
<p>b.   <input type="text" name="1.15b" id="1.15b" size= "117" value="answer15b"></p>
<p>c.   <input type="text" name="1.15c" id="1.15c" size= "117" value="answer15c"></p>

</form>
<button onclick="getFormData()">getFormData</button>
</div>

顺便说一句:而不是过时的

<script language="Javascript"></script>

你应该使用

<script type="text/javascript"></script>

为了让文件中的文字更易读,可以使用JSON.stringify的第三个参数。

JSON.stringify(ret_obj, null, '\t')

更新

在上面的示例中,输入字段中未回答的问题根本不是 安全。要要求答案,您可以使用 属性 required

<!-- example for required attribute of input element -->
<h4>11. Respirators should be used to prevent?</h4>
<p><input type="text" name="1.11" id="1.11" size= "120" required></p>

但是单选按钮的问题是必需的,否则脚本会抛出错误,因为在该行

ret_obj[item.name] = document.querySelector('input[name="' + item.name + '"]:checked').value;

document.querySelector('input[name="' + item.name + '"]:checked')null 如果没有选中该组的单选按钮并且 null 没有属性 value

作为 w3.org states:

为避免混淆是否需要单选按钮组,鼓励作者在组中的所有单选按钮上指定属性。实际上,一般来说,鼓励作者首先避免使用没有任何初始检查控件的单选按钮组,因为这是用户无法返回的状态,因此通常被认为是糟糕的用户界面。

事实上,一个组中只有一个单选按钮需要属性required 来使该组成为必需的。或者应该有一个预选的单选按钮,如下例所示。

<h4>1. Who is the owner of my socks?</h4>
<p><label for="q1a">
  <input type="radio" name="socksOwner" value="me" id="q1a">me
</label></p>
<p><label for="q1b">
  <input type="radio" name="socksOwner" value="JohnDoe" id="q1b" />John Doe
</label></p>
<p><label for="q1c">
  <input type="radio" name="socksOwner" value="NA" id="q1c" checked/>I don't know
</label></p>

但是,如果您不希望对带有单选按钮的问题的回答是必需的或预先选择的,我们需要在脚本中进行处理。因此,我们检查是否选择了一个组中的一个项目,并且只有在这种情况下才是安全的值。为此,请更改此行

ret_obj[item.name] = document.querySelector('input[name="' + item.name + '"]:checked').value;

到这里:

/* checked item in radio group*/
var selRadio = document.querySelector('input[name="' + item.name + '"]:checked');
/* if one of the radio buttons in the group is checked, safe value */
if (selRadio !== null) {
  ret_obj[item.name] = selRadio.value;
}

【讨论】:

  • 感谢您的帮助,我需要它!我打算让学生下载它们,这样他们就可以通过 Office 365 帐户将它们作为附件通过电子邮件发送给我。
  • 如果我的回答有帮助,请考虑支持/接受。
  • 不要忘记把我注释掉的下载功能中的部分再注释掉。
  • 如果学生可以在不回答所有问题的情况下保存文件。未回答的问题不在文件中。为了使输出更好的可读性,您可以使用JSON.stringify 的第三个参数。我将在我的答案中添加一个示例。
  • 对不起,我不明白。如果您还有其他问题,请提出新问题。如果您愿意,可以在此处添加指向新问题的链接。
猜你喜欢
  • 2010-09-14
  • 1970-01-01
  • 2019-02-20
  • 2023-01-24
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多