只有选中复选框并提交表单时,才能下载文件
Download a file only if a checkbox is checked and form submits
我希望只有当用户选中复选框并提交表单时才能下载PDF。现在只要选中复选框并点击提交按钮,PDF就会下载。表格是否提交并不重要。主要问题是我仅限于Jquery或纯javascript。我没有访问后端文件的权限。有人知道做这件事吗?不需要复选框。这是我现在的代码:
$("#formid").submit(function(ev){
ev.preventDefault();
$.ajax({
url: 'processing.cfc', // background processing procedures
type: 'get',
dataType: 'json',
data: $("#formid input, #formid select").serialize(), //pass all present input variables
success: formReturn,
failure: function(){ alert("There was an error processing your request. 'nPlease try again."); return false; }
});
var $choice = $(this).find("input[name='checkbox1']:checked");//get the selected option
if ($choice.length)// if an option is selected
window.open('http://whitepaper.com/info/whitepaper/WhyBuy_WhitePaper_FinalB.pdf') ;
var $choice2 = $(this).find("input[name='checkbox2']:checked");//get the selected option
if ($choice2.length)// if an option is selected
window.open('http://brochure.com/info/brochure/welcome-kit-brochure.pdf') ;
});
这是复选框的HTML:
<div class="chkbox">
<input type="checkbox" class="regular-checkbox" name="checkbox1"
id="checkbox-1-1"><label for="checkbox-1-1"></label>
<span class="dld">Clayton Homes guide to buying a home</span>
</div>
<div class="chkbox">
<input type="checkbox" class="regular-checkbox" name="checkbox2"
id="checkbox-1-2"><label for="checkbox-1-2"></label>
<span class="dld">Why Clayton Homes brochure</span>
</div>
感谢您的帮助。
您必须移动PDF打开AJAX函数的成功调用。
这里的关键是:AJAX调用是异步的,所以PDF将在AJAX请求被激发后立即打开,而不是在响应到达后打开。类似这样的东西:
$("#formid").submit(function (ev) {
ev.preventDefault();
$.ajax({
url: 'processing.cfc', // background processing procedures
type: 'get',
dataType: 'json',
data: $("#formid input, #formid select").serialize(), //pass all present input variables
success: function () {
var $choice = $("#formid input[name='checkbox1']:checked"); //get the selected option
if ($choice.length) { // if an option is selected
window.open('http://whitepaper.com/info/whitepaper/WhyBuy_WhitePaper_FinalB.pdf');
} else {
var $choice2 = $("#formid input[name = 'checkbox2']:checked "); //get the selected option
if ($choice2.length) { // if an option is selected
window.open('http://brochure.com/info/brochure/welcome-kit-brochure.pdf');
}
}
},
failure: function () {
alert("
There was an error processing your request.'nPlease
try again.");
return false;
}
});
});
},
failure: function () {
alert("There was an error processing your request. 'nPlease try again.");
return false;
}
});
});
`<input type="checkbox" name="checkboxG1m" id="checkbox_a" class="css-checkbox" />`
`$`("#checkbox_a").click(function(){
if($("#checkbox_a").is(':checked')){
window.open("x.pdf");
}
});
相关文章:
- asp.net网站文件下载历史记录
- Firefox,如何提交表单触发文件下载,但停留在网页上
- Interent Explorer中的数据URI文件下载
- 将页面上的文本替换为 jQuery 并提示文件下载
- JavaScript文件下载
- 是否可以触发文件下载到用户的浏览器
- Javascript JSON 到 Excel 文件下载
- 使用Javascript将多个文件下载为zip文件或文件夹
- 巴比伦JS-场景从本地文件下载
- 文件下载html与错误处理FileNotFound
- 使用jQuery捕获站点范围内的文件下载
- 在WinJS中将文件下载到文件系统
- JQuery启动文件下载,然后运行一个函数
- 文件下载无法在firefox上运行
- 创建文件下载js
- 如何使用JQuery禁用pdf文件下载选项
- 使用JavaScript/Jquery创建文本文件下载/更新到本地客户端机器中的特定位置
- CollectionFS中是否存在用于文件下载的事件侦听器/回调
- 文件下载后,Document Ready无法工作
- 浏览器不生成文件下载对话框