当访问者提交经过验证的选择加入表单时,我正在尝试创建自动文件下载。我想使用 Javascript 函数作为onsubmit()
事件来启动下载。这是我正在使用的代码我已经在使用表单action=
将表单数据提交到数据库,并且已经有 Javascript 来验证表单。我只需要添加下载功能。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css" xml:space="preserve">
BODY, P,TD{ font-family: Arial,Verdana,Helvetica, sans-serif; font-size: 10pt }
A{font-family: Arial,Verdana,Helvetica, sans-serif;}
B { font-family : Arial, Helvetica, sans-serif; font-size : 12px;font-
weight
: bold;}
.error_strings{ font-family:Verdana; font-size:14px; color:#660000;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">
</script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script language="JavaScript" src="gen_validatorv4.js"
type="text/javascript" xml:space="preserve"></script>
<script type="text/javascipt">
var downloadURL = function downloadURL(url) {
var iframe;
var hiddenIFrameID = 'hiddenDownloader';
iframe = document.getElementById(hiddenIFrameID);
if (iframe === null) {
iframe = document.createElement('iframe');
iframe.id = hiddenIFrameID;
iframe.style.display = 'none';
document.body.appendChild(iframe);
}
iframe.src = 'http://healthyweightnaturally.com/file-download/download.php?
download_file=integrative_nutrition_ebook.pdf';
}
</script>
</head>
<body>
<form name="myform" id="myform" method="post"
action="https://www.example.com/FormHTML.aspx" onSubmit="return downloadURL(url);">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">
First Name
</td>
<td>
<input type="text" name="FirstName" />
</td>
</tr>
<tr>
<td align="right">
Last Name
</td>
<td>
<input type="text" name="LastName" />
</td>
</tr>
<tr>
<td align="right">
Email *
</td>
<td>
<input type="text" name="Email" />
</td>
</tr>
<tr>
<td align="right"></td>
<td>
<div id="myform_errorloc" class="error_strings">
</div>
</td>
</tr>
<tr>
<td align="right"></td>
<td>
<input style="padding-left:10px;" src="http://healthyweightnaturally.com/file-
download/download-button.png" type="image" value="submit" value="Submit" />
</td>
</tr>
</table>
<div style="visibility:hidden">
<iframe name="ifr1" width="20" height="20" id="hiddenIFrameID"></iframe>
</div>
</form>
<script language="JavaScript" type="text/javascript"
xml:space="preserve">//<![CDATA[
//You should create the validator only after the definition of the HTML form
var frmvalidator = new Validator("myform");
frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("FirstName","req","Please enter your First Name");
frmvalidator.addValidation("FirstName","maxlen=20", "Max length for FirstName
is 20");
frmvalidator.addValidation("FirstName","alpha_s","Name can contain alphabetic chars
only");
frmvalidator.addValidation("LastName","req","Please enter your Last Name");
frmvalidator.addValidation("LastName","maxlen=20","For LastName, Max length is 20");
frmvalidator.addValidation("Email","maxlen=50");
frmvalidator.addValidation("Email","req");
frmvalidator.addValidation("Email","email");
//]]>
</script>
</body>
</html>
我想使用onsubmit
而不是onclick
因为我需要确保在能够下载文件之前表单已经过验证。我对 Javascript 有点陌生,并且一直在努力解决这个问题。我将非常感谢某人的帮助。我找到的最接近答案的东西可以在HTML OnSubmit: Download OR HTML中看到,但由于他们从未提供任何代码示例,因此无法帮助我查看解决方案。