只有选中复选框并提交表单时,才能下载文件

Download a file only if a checkbox is checked and form submits

本文关键字:文件 下载 表单 提交 复选框      更新时间:2023-09-26

我希望只有当用户选中复选框并提交表单时才能下载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>&nbsp;&nbsp;
  <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>&nbsp;&nbsp;
  <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");
    }
});