gpt4 book ai didi

html - 将文本文件的 html 输入下载到 C 盘本地文件夹

转载 作者:可可西里 更新时间:2023-11-01 13:19:20 24 4
gpt4 key购买 nike

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

这适用于 outlook 但学生没有 outlook 他们有 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>

最佳答案

如果我没看错你的问题,问题是你用未定义的参数调用了下载函数。要从您的表单中获取数据,您可以遍历

document.getElementById('yourFrom').elements

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

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

getFormData()

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

由于您的问题表单中有单选按钮,循环应该检查它和安全的只有选定的值。我在我的示例代码中添加了注释解释这是如何完成的。

我把函数注释掉了

download()

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

console.log(...);

为了方便起见,我还在代码片段中的表单中放置了一些示例值。

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')

更新

在上面的示例中,输入字段中未回答的问题根本就不是安全。要要求回答,您可以使用<input> 属性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 没有属性

作为 w3.org states :

To avoid confusion as to whether a radio button group is required or not, authors are encouraged to specify the attribute on all the radio buttons in a group. Indeed, in general, authors are encouraged to avoid having radio button groups that do not have any initially checked controls in the first place, as this is a state that the user cannot return to, and is therefore generally considered a poor user interface.

事实上,只有一个组的单选按钮需要属性 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;
}

关于html - 将文本文件的 html 输入下载到 C 盘本地文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54957665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com