【发布时间】:2018-08-06 21:34:50
【问题描述】:
下面的代码非常适合下载文本框。但是,我希望扩展它以添加三个框和一个单选按钮。然后,我希望能够下载包含所有数据的 .txt。例如,名字、姓氏、电子邮件、男/女(单选按钮)。 我似乎找不到这样做的方法。非常感谢任何帮助!
<!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 type in this box. When you
click the Download button, the contents of this box will be downloaded to
your machine at the location you specify. Pretty nifty. </textarea>
<input type="submit" value="Download">
</form>
</body>
【问题讨论】:
-
您可以在文本中添加单选按钮数据数据,然后再将其传递给您的 download() 函数。
-
您需要做的就是从 DOM 中获取所有数据。将其添加在一起然后格式化..然后将其传递给您的下载功能。
标签: javascript html forms text radio