- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我刚开始,我们学生和我创建了一个在线工作簿,但无法下载到文本文件的答案,它下载了,但是当我们打开文件时,它出现了未定义这是我的代码。
这适用于 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/
我有一个测试即将进行,我想澄清两个有关参数的问题。 在我的笔记中指出,将参数传递给函数的推荐方法是使用“按引用传递” const type& x; // for "in" parameters
当我通过 OMG 2.5(Beta)推广的 UML 规范阅读以下概念时: in: Indicates that Parameter values are passed in by the caller
我试图在用户按下 Enter 时触发一个函数。我将此输入设置为只读的原因是限制用户在填充值后修改输入中的值。 该值来自将在点击属性中触发的弹出窗口。问题是 keyup.enter 没有触发该输入。 代
我在jQuery中使用模式弹出窗口控件,该弹出窗口具有由jQuery Tokenize输入插件提供动力的输入文本。问题是,当我在模式弹出文本框中键入内容时, token 化插件的搜索结果显示为隐藏在弹
我有一个问题。当我选中复选框时,系统工作正常,总值发生变化,但一旦我修改文本输入,它就会变为 0。我需要将此文本框输入排除在更改值之外。 这是 html: $15000 $
我正在努力让它发挥作用,但还是有些不对劲。 我想用 CSS 设置提交按钮的样式以匹配我已有的按钮。 风格: input[type="button"], input[type="submit"], b
import java.util.*;; public class selection { Scanner in=new Scanner(System.in); private
这可能是一个非常菜鸟的问题。假设我有一个带宽限制为 100MB/s 的网卡,那么输入/输出带宽是否有可能达到该限制 同时 ?或者我会在任何时候遇到这个不等式:in bandwidth + out ba
看着这个问题,Fill immutable map with for loop upon creation ,我很好奇是什么this表示在 Map(1 -> this) . scala> Map(1
我有这样的东西 一个 乙 问? 是或否 数字 数字或零 我想做的是: 如果 B1 = “Y”,则让用户在 B2 中输入一个数字。 如果 B1 = “N”,则将 B2 中的值更改为零,并且不允许用户在
我有一个包含许多列的表,我想添加 input标题单元格内的字段,但我希望输入适合根据正文内容的宽度。 这是没有 input 的样子领域: 这就是 input 的样子领域: 可以看出,像“index”和
关于为 FTP 客户端设置传出和传入文件夹,您遵循哪些最佳实践(如果有)?我们通常使用“outgoing”和“incoming”,但无论你如何表述方向,它都可以有两种解释方式,具体取决于名称相对于哪一
我正在尝试“求解”给定 d 的 Pell 方程:x^2 - d * y^2 = 1,或者至少我想得到最小的 x > 0 来求解方程。到目前为止,一切都很好。这是我的 Haskell 代码 minX :
我是VS Code的新手,可以使用Ctrl + Enter将代码运行到python交互式窗口中。我希望光标自动移动到下一行,因此我可以逐行浏览代码。 能做到吗? 最佳答案 如this blog pos
我正在创建一个 bool 值矩阵/二维数组,并且我想为 dategrid 推断一种不仅仅是“ANY”的类型。 let yearRange = [2000,2001,2002,2003,2004]; l
我有两个排序的列表,例如 a = [1, 4, 7, 8] b = [1, 2, 3, 4, 5, 6] 我想知道a中的每个项目是否在b中。对于上面的示例,我想找到 a_in_b = [True, T
菜鸟警报 这很奇怪 - 当我编写以下代码时,尝试在 AngularJS 中创建自定义指令: myModule.directive('myTab', function(){ console.lo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
假设我正在使用 gdscript 静态类型,并且对于一个函数参数,我事先不知道我会得到什么。这就是 python 中 typing.Any 的作用。如何使用 gdscript 做到这一点? 似乎 Va
我使用 dropzone 上传多个图像,并且工作正常,直到我想为每个图像插入品牌和网址。 我遇到的唯一问题是,当我要从输入字段获取值时,我会从服务器获取来自字段(品牌、网址)的未定义值,但如果我使用静
我是一名优秀的程序员,十分优秀!